#!/usr/bin/python
# -- coding:utf8 --
import math
import pandas as pd
from pandas import DataFrame,Series
import numpy as np
import pymysql
import json
import datetime
import re
from pathlib import Path
from math import sin, asin, cos, radians, fabs, sqrt
from datetime import  timedelta, timezone
import pymysql
import json
import numpy as np
import datetime,time
import unidecode
import requests,json,time,random
from collections import Counter

import os
import numpy as np
import pandas as pd
import lightgbm as lgb
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import roc_auc_score
# from bayes_opt import BayesianOptimization
import pickle

import warnings
warnings.filterwarnings('ignore')



pd.options.display.max_rows=1000
pd.options.display.max_columns=1000
pd.options.display.width=1000


from sklearn.metrics import roc_curve, auc
# import matplotlib.pyplot as plt

from lightgbm.sklearn import LGBMRegressor
from lightgbm.sklearn import LGBMClassifier
# from sklearn.grid_search import GridSearchCV

import datetime
import os
from sklearn.model_selection import KFold
from collections import Counter
# import unidecode
# from hyperopt import fmin, tpe, hp
# import requests,json,time,random

def cal_strdate_to_int(apply_time):  # 13位时间戳  # '2024-01-25 23:44:47' ->  1706247887000
    s_t  = time.strptime(str(apply_time), "%Y-%m-%d %H:%M:%S")
    return int(time.mktime(s_t))*1000

def cal_strdate_to_datetime(apply_time):  # '2024-01-25 23:44:47' ->  datetime.datetime(2024, 1, 25, 23, 44, 47)
    s_t = time.strptime(str(apply_time), "%Y-%m-%d %H:%M:%S")
    s_t = int(time.mktime(s_t)) * 1000
    date_time = datetime.datetime.fromtimestamp(s_t / 1000)
    return date_time

def cal_strdate_to_datetime_02(apply_time):  # '2024-01-25' ->  datetime.datetime(2024, 1, 25, 23, 44, 47)
    s_t = time.strptime(str(apply_time), "%Y-%m-%d")
    s_t = int(time.mktime(s_t)) * 1000
    date_time = datetime.datetime.fromtimestamp(s_t / 1000)
    return date_time


def cal_int13_to_strdate(install_timeStamp):  # 时间戳计算 返回 年月日
    create_time = datetime.datetime.fromtimestamp(float(install_timeStamp)/1000)
    return create_time



def tmp_d(fz,fm):
    if  fm==0 or not fm  :
        return -999
    else:
        return round(fz / fm, 4)


def cal_timestamp(install_timeStamp):  # 时间戳计算 返回 年月日
    create_time = datetime.datetime.fromtimestamp(float(install_timeStamp)/1000)
    return create_time


# 指定时区计算时间戳
def cal_timestamp_utc(install_timeStamp,hour):  # 时间戳计算 返回 年月日
    td = timedelta(hours=hour)
    tz = timezone(td)
    create_time = datetime.datetime.fromtimestamp(float(install_timeStamp)/1000, tz)
    return create_time


def cal_hours(a,b):
    tmp = a-b
    hours = tmp.days*24+tmp.seconds/3600
    return hours



def tmp_time(a):
    a = datetime.strptime(a, '%d/%m/%Y %H:%M:%S')  # datetime.datetime(2022, 9, 8, 5, 55, 11)
    a.strftime("%Y-%m-%d")
    return a


def cal_boy_type(x, type):
    y = 0
    if re.search(type, x):
        y = 1
    return y



def cal_divide(x, y):
    if x == 0 and y == 0:
        value = -2
    elif y == 0:
        value = -1
    else:
        value = round(x / y, 4)
    return value


def is_phone(x):
    phone = None
    x = re.sub("\D", "", str(x).lower())
    if len(x)==10:
        phone = x
    elif len(x)==12 and x[:2]=='52':
        phone = x[2:]
    elif len(x)==13 and x[:3]=='521':
        phone = x[3:]
    return phone


def cal_wj_isBorrowedFar(x):
    x=str(x)
    if  x == 'Mas de 10':
        y=11
    elif x =='Ninguno' or x=='None':
        y=0
    else:y=int(x)
    return y

def read_topcash(x,sms_topcash_key):
        tmp = re.search(sms_topcash_key, x)
        start =  tmp.start()
        end = tmp.end()
        name = x[start:end]
        return name

def cal_minmax(x,type):
        y=None
        if x:
            if type=="min":
                y = min(x)
            elif type=="max":
                y = max(x)
        return y


# 剔除非数字部分，但不剔除.
def cal_balance(x): # 不含 Rs INR
    y = None
    x = x.replace(',', '')
    x = x.replace(' ', '')
    position = x.find('.')
    if position==0:
        y = x[1:]
        y = float(y)
    return y


def cal_ind_bank_amount(x):
    y=0
    x = x.replace(',', '')
    x = x.replace(' ', '')
    position = re.search('Rs|INR', x)
    if not position:
        return y
    x = x[position.end():position.end() + 6]
    i=0
    for s in x:
        if s==".":
            i+=1

    if i == 0 :
        y = re.sub("\D", "", x)
    elif i == 1:
        p = x.find('.')
        if p==0:
            y = x[p+1:]
            y = re.sub("\D", "", y)
        else:
            y = x[:p]
            y = re.sub("\D", "", y)

    elif i==2:
        x = re.sub(" ", "", x)
        p = x.find('.')
        x = x[p + 1:]
        p = x.find('.')
        y = x[:p]
        y = re.sub("\D", "", y)

    if y:
        y = float(y)
    else:y=0

    return y


def cal_bl_amount(x):
    y=None

    x = x.replace(',', '')
    x = x.replace(' ', '')

    position = re.search('s/', x)
    if not position:
        return y
    else:
        x = x[position.end():position.end() + 8]
        position = re.search('\.', x)
        if position:
            x = x[:position.start()]
            x = re.sub("\D", "", x)

        else:
            x = re.sub("\D", "", x)
    if x:
        y = float(x)
    return y


def is_phone_ind(x):
    x = re.sub("\D", "", str(x).lower())
    if len(x)==10 and x[0] in ['6','7','8','9']:
        y = x
    elif len(x)== 12 and x[:2]== '91' and x[2] in ['6','7','8','9']:
        y = x[2:12]
    else:y=None
    return y


def phone_extract(info,phone_key):
    tmp = re.search(phone_key, info.lower())
    if tmp :
        p_end = tmp.end()
        extract_phone = info[p_end:p_end+15]
        extract_phone = re.sub("\D", "", extract_phone)
        extract_phone = is_phone_ind(extract_phone)
        return extract_phone
    else:
        return None

def tmp_d(fz,fm):
    if  fm==0 or not fm  :
        return -999
    else:
        return round(fz / fm, 4)

def del_http(x):
    x = str(x)
    location = x.find("http")
    return x[0:location]


def cal_sms_content(x):
    x = str(x)
    x = x.lower()
    x = re.sub(r'[^a-zA-Z0-9 %]', '', x)
    x = re.sub("lon", "loan", x)
    x = re.sub("ioan", "loan", x)
    x = re.sub("csh", "cash", x)
    x = re.sub("u0005", "", x)
    x = re.sub("u001b", "", x)
    x = re.sub("cah", "cash", x)
    x = re.sub("pes0", "peso", x)
    x = re.sub(" ccount", " account", x)
    x = re.sub("ovedue","overdue",x)  # collectio
    x = re.sub(" collectio "," collection ",x)  # collectio
    # 正则更简单
    x = re.sub("days", " day", x)

    x = re.sub(' {2,}', ' ', x)  # 空格>=2 替换成1个
    x = x.lstrip()
    return x


def tq_xw(x):
    x = str(x)
    x = x.strip()
    x = unidecode.unidecode(x.lower())
    pattern = "$(.*?)$|<(.*?)>|【(.*?)】|\[(.*?)\]|\((.*?)\)|\{(.*?)\}|（(.*?)）|(^[^0-9]{0,15}):|(^[^0-9]{0,15})：|\*\*(.*?)\*\*"
    matches = re.search(pattern, x)
    y = "-999"
    if matches:
        s = matches.start()
        e = matches.end()
        y = x[s:e]
        y = re.sub(r'[^a-zA-Z0-9áéíóúüÁÉÍÓÚÜ]', '', y)  # 保留数字英文和空格

        # 新增逻辑
        # 兜底

        y = re.sub("ioan", "loan", y)
        y = re.sub("lon", "loan", y)
        y = re.sub("csh", "cash", y)
        y = re.sub("u0005", "", y)
        y = re.sub("u001b", "", y)
        y = re.sub("cah", "cash", y)

        y = y.strip()

        # 删除结尾的
        if re.search("vip\d+$", y):
            tmp = re.search("vip\d+$", y)
            len_tmp = len(tmp.group(0))
            y = y[:-len_tmp]
        if re.search("ph$", y):
            y = y[:-2]
        if re.search("vip$", y):
            y = y[:-3]
        if re.search("ios$", y):
            y = y[:-3]

        # 删除开头
        if re.search("^support", y):
            y = y[len("support"):]


        if 'bene' in y:
            y = 'bene'
        if "moregold" in y:
            y = "moregold"
        if "moregold" in y:
            y = "moregold"
        if "peramo" in y:
            y = "peramo"
        if "tala" in y:
            y = "tala"
        if "negosyoko" in y:
            y = 'negosyoko'
        if "bpi" in y:
            y = 'bpi'

    return y


def cal_regular_simple(x):
    x = str(x).lower()
    x = re.sub("ngayong", "ngayon", x)
    x = re.sub("ngayung", "ngayon", x)
    x = re.sub("due date", "duedate", x)
    x = re.sub("reminder", "remind", x)
    x = re.sub("na po ang", "is", x)
    x = re.sub("po ang", "is", x)
    x = re.sub("napo an", "is", x)
    x = re.sub("ang araw ng", "is", x)
    x = re.sub("reminds", "remind", x)
    x = re.sub("na ang", "is", x)
    x = re.sub("nyo na po", "is", x)
    x = re.sub("na ang", "is", x)
    x = re.sub("paalala", "remind", x)
    x = re.sub("araw ang", "is", x)  # ngayun
    x = re.sub("ngayun", "ngayon", x)  # ngayun

    x = re.sub("paalala", "remind", x)
    return x

# 封装代码的函数库
def cal_strdate_to_int(apply_time):
    s_t  = time.strptime(apply_time, "%Y-%m-%d %H:%M:%S")
    return int(time.mktime(s_t))*1000

def cal_strdate_to_datetime(apply_time):
    s_t = time.strptime(str(apply_time), "%Y-%m-%d %H:%M:%S")
    s_t = int(time.mktime(s_t)) * 1000
    date_time = datetime.datetime.fromtimestamp(s_t / 1000)
    return date_time

def tmp_d(fz,fm):
    if  fm==0 or not fm  :
        return -999
    else:
        return round(fz / fm, 4)



def p2score(p, pdo=40, base_score=700):
        """
        概率转换为分数的函数
        说明：概率p=坏的概率
        :param p:
        :param pdo:
        :param base_score:
        :return:
        """
        b = pdo / np.log(2)
        a = base_score + b * np.log(1 / 20)
        if p < 0:
            return -99999
        min_p = 1e-9
        max_p = 1 - min_p
        if p >= max_p:
            p = max_p
        elif p <= min_p:
            p = min_p
        score = int(a + b * np.log((1 - p) / (p)))
        if score < 300:
            return 300
        elif score > 900:
            return 900
        else:
            return score

def cal_sms_content(x):
    x = str(x)
    x = x.lower()
    x = re.sub(r'[^a-zA-Z0-9 %]', '', x)
    x = re.sub("lon", "loan", x)
    x = re.sub("ioan", "loan", x)
    x = re.sub("csh", "cash", x)
    x = re.sub("u0005", "", x)
    x = re.sub("u001b", "", x)
    x = re.sub("cah", "cash", x)
    x = re.sub("pes0", "peso", x)
    x = re.sub(" ccount", " account", x)
    x = re.sub("ovedue","overdue",x)  # collectio
    x = re.sub(" collectio "," collection ",x)  # collectio
    # 正则更简单
    x = re.sub("days", " day", x)

    x = re.sub(' {2,}', ' ', x)  # 空格>=2 替换成1个
    x = x.lstrip()
    return x

def get_ph_reg(request = None):

    if request:
        ph_reg = request.app.state.jsons.get('ph_reg_20240715.json', None)
        # print(ph_reg)
    else:
        ph_reg = None
        
    if ph_reg is not None:
        # 使用 ph_reg_data
        # print(ph_reg)
        pass
    else:
        file_path_name = './model/ph_reg_20240814.json'
        current_path = os.path.dirname(__file__)
        file_path_name = file_path_name.replace("./", current_path + "/")
        with open(file_path_name, 'r') as f:
            ph_reg = json.load(f)
    
    return ph_reg

def get_pickle(file_name, request = None):
    if request:
        pkl = request.app.state.models.get(file_name, None)
    else:
        pkl = None
    
    if pkl is not None:
        pass
    else:
        file_path_name = './model/'+file_name
        current_path = os.path.dirname(__file__)
        file_path_name = file_path_name.replace("./", current_path + "/")
        file_path_name = file_path_name.replace("\\\\", "/")
        with open(file_path_name, 'rb') as f:
            pkl = pickle.load(f)
    
    return pkl

def load_data_from_pickle(file_path_name):
    """
    加载pkl格式数据
    :param file_path_name:
    :return:
    """
    current_path = os.path.dirname(__file__)
    file_path_name = file_path_name.replace("./", current_path + "/")
    file_path_name = file_path_name.replace("\\\\", "/")
    with open(file_path_name, "rb") as infile:
        result = pickle.load(infile)
    return result

def fix_file_path(file_path_name):
    # 如果是绝对路径,则直接返回
    if os.path.isabs(file_path_name):
        return file_path_name
    
    # current_path = os.path.dirname(__file__)
    # file_path_name = file_path_name.replace("./", current_path + "/")
    # file_path_name = file_path_name.replace("\\\\", "/")
    # return file_path_name
    
    # 处理相对路径
    current_path = Path(os.path.dirname(__file__))
    file_path = current_path / file_path_name.lstrip("/")
    return str(file_path.resolve())

def load_pickle(file_path_name):
    
    file_path_name = fix_file_path(file_path_name)
    
    # current_path = os.path.dirname(__file__)
    # file_path_name = file_path_name.replace("./", current_path + "/")
    # file_path_name = file_path_name.replace("\\\\", "/")
    
    pkl = pickle.load(open(file_path_name, 'rb'))
    return pkl


# 计算逾期天数 如果数据库计算不出来用当前时间计算
def cal_overdue_days_xw(info):
    should_date = info["ReDate"]
    pay_date = info["CompleteDate"]
    should_date = str(should_date)[:10]
    late_day = -999
    if pay_date == "NaT" or  pay_date == None:
        should_date = datetime.datetime.strptime(should_date, '%Y-%m-%d')
        now = datetime.datetime.strptime(str(datetime.datetime.now())[:10], '%Y-%m-%d')
        late_day = (now - should_date).days
    if pay_date != "NaT" and pay_date != None :
        pay_date = str(pay_date)[:10]
        pay_date = datetime.datetime.strptime(pay_date, '%Y-%m-%d')
        should_date = datetime.datetime.strptime(should_date, '%Y-%m-%d')
        late_day = (pay_date - should_date).days
    return late_day




def tmp_time(a):
    a = datetime.strptime(a, '%d/%m/%Y %H:%M:%S')  # datetime.datetime(2022, 9, 8, 5, 55, 11)
    a.strftime("%Y-%m-%d")
    return a

def cal_strdate_to_int(apply_time):
    s_t  = time.strptime(apply_time, "%Y-%m-%d %H:%M:%S")
    return int(time.mktime(s_t))*1000
def cal_strdate_to_datetime(apply_time):
    s_t = time.strptime(str(apply_time), "%Y-%m-%d %H:%M:%S")
    s_t = int(time.mktime(s_t)) * 1000
    date_time = datetime.datetime.fromtimestamp(s_t / 1000)
    return date_time

def cal_boy_type(x, type):
    y = 0
    if re.search(type, x):
        y = 1
    return y

def regular_extraction(org_content, x):
    x = str(x).lower()
    matches = re.search(org_content, x)
    y = "-999"
    if matches:
        s = matches.start()
        e = matches.end()
        y = x[s:e]
    return y

def cal_sms_org_all(sms_org_list,sms_org_pattern,sms_content,contactor_name):
    sms_content_ok = cal_sms_content(sms_content)
    contactor_name = str(contactor_name).lower()
    contactor_name = re.sub(r'[^a-z0-9]', '', contactor_name)
    sms_org = tq_xw(sms_content)
    if len(sms_org) <= 0:
        sms_org = "-999"
    sms_list_org = [x for x in sms_org_list if x == sms_org or x == contactor_name]
    if len(sms_list_org) >= 1:
        y = sms_list_org[0]
    else:
        y = '-999'
    if y == '-999':
        y = regular_extraction(sms_org_pattern, sms_content_ok +" "+ contactor_name)
        y = re.sub(' ', '', y)  # 去空格
    return y


sms_topcash_key = 'mabiliscash|creditpeso|homecredit|tala|mocamoca|gopeso|digido|juanhand|pesoloan|onlineloans|okpes|lpeso|pesocash|fastcash|cashalo'
sms_1m_cash_key  = 'valleyloan|fastcash|maypera|wowpera|lemonloan|pesohere|zippeso|pesoredee|easypeso|mrcash|peramoo|cashme|moneycat|upeso|mocasa|cashbee|mabiliscash|creditpeso|homecredit|tala|mocamoca|gopeso|digido|juanhand|pesoloan|onlineloans|okpes|lpeso|pesocash|fastcash|cashalo'


# df_sample = pd.read_parquet(r"C:\Users\12435\project\xx\跟放产品-新客-20240807.parquet")
# df_sample.fillna("-999", inplace=True)
# print(df_sample.shape)

# df_sample = pd.merge(df_sample[['ApplyNO',"FileUrl","Birthday","Maritalstatus"]], df_apply_all, on='ApplyNO')
# print(df_sample.shape)

# df_sample["daoqi_day"] = df_sample["ReDate"].map(lambda x: str(x)[:10])
# df_ok = df_sample[df_sample['daoqi_day']=='2024-08-07']
# print(df_ok.shape)

# df_ok_tmp = df_ok[df_ok['ApplyNO']=='172174179393897325'] # 172174179393897325
# print(df_ok_tmp.shape)

def cal_genfang_new_v1(params):
    start_time = time.time()
    # 本地关键词信息
    app_del_key = "google|Calculator|Samsung|Microsoft|miui|realme|Xiaomi|com.mi.global|lenovo"
    app_del_key = app_del_key.lower()

    data = get_ph_reg(request=None)
    package_id_2m = data['package_id_2m']
    package_id_1m = data['package_id_1m']
    package_id_50w = data['package_id_50w']
    package_id_loan = data['package_id_loan']
    package_id_48 = data['package_id_48']
    package_id_46 = data['package_id_46']
    package_id_44 = data['package_id_44']
    package_id_42 = data['package_id_42']
    sms_org_pattern = data['sms_org_pattern']
    bank_org_pattern = data['bank_org_pattern']
    sms_org_list = data['sms_org_list']
    app_del_key = data['app_del_key']
    sms_dele_key = data['sms_dele_key']
    sms_verif_key = data['sms_verif_key']
    sms_cash_key = data['sms_cash_key']
    sms_loan_key = data['sms_loan_key']
    sms_repayment_key = data['sms_repayment_key']
    sms_expire_key = data['sms_expire_key']
    sms_overdue_key = data['sms_overdue_key']
    sms_overdue_serious_key = data['sms_overdue_serious_key']
    sms_bill_key = data['sms_bill_key']

    # 线下测试的参数集
    # order_id = params["ApplyNO"]
    # apply_time = params["ApplyDate"]
    # birthday = params["Birthday"]
    # degree = params["degree"]

    # app_data = params["AppList"]
    # sms_json = params["SmsList"]
    # ProjectId = params['ProjectId']
    # ApplyDate = str(params['ApplyDate'])
    # created_at = str(params['ApplyDate'])
    # 手机号码
    # id_number = params['Phone']

    # 线上部署的参数集
    order_id = params["order_id"]
    apply_time = params["apply_time"]
    birthday = params["birthday"]
    degree = params["degree"]
    app_data = params["app_data"]
    sms_json = params["sms_json"]
    ProjectId = params['ProjectId']
    ApplyDate = str(params['apply_time'])
    created_at = str(params['apply_time'])

    # 手机号码
    id_number = params['id_number']
    # 老客数据
    id_number_json = params['id_number_json']
    # 判断是否是字符串
    if isinstance(id_number_json, str):
        id_number_json = json.loads(id_number_json)
    # pandas dataframe
    df_apply_all = pd.DataFrame(id_number_json)
    # print(df_apply_all)
    # print(df_apply_all.shape)
    # 稍微清洗几个数据
    df_apply_all["id_number"] = id_number
    df_apply_all['fact_time'] = df_apply_all['CompleteDate'].map(lambda x: str(x))
    df_apply_all['fact_day'] = df_apply_all['CompleteDate'].map(lambda x: str(x)[:10])
    df_apply_all['apply_time_tmp_day'] = df_apply_all['ApplyDate'].map(lambda x: str(x)[:10])
    df_apply_all["created_at"] = df_apply_all["ApplyDate"].map(lambda x: str(x))
    df_apply_all["order_id"] = df_apply_all["ApplyNO"].map(lambda x: int(x))
    df_apply_all["loan_time"] = df_apply_all["CreateDate"].map(lambda x: str(x))
    df_apply_all["plan_repayment_amount"] = df_apply_all["Amount"]
    df_apply_all['overdue_days_xw'] = df_apply_all.apply(cal_overdue_days_xw, axis=1)

    # order_id = params["order_id"]
    # ApplyDate = params["ApplyDate"]
    apply_time = params["apply_time"]
    apply_time = str(apply_time)
    apply_time_int = cal_strdate_to_int(str(apply_time))
    apply_time_datetime = cal_strdate_to_datetime(apply_time)

    # FileUrl = params["FileUrl"]

    """ 1 用户基本信息模块特征集 """
    y = params["birthday"]
    x = apply_time
    y = str(y)[:4]
    x = str(x)[:4]
    age = int(x)-int(y)
    Maritalstatus = params["Maritalstatus"]

    """  2 app列表模块特征集 """
    if isinstance(app_data, str):
        app_data = json.loads(app_data)

    app_data = [x for x in app_data if x['appName'] and x['packageName']
                and x['is_system'] == "0" and x['firstInstallTime'] >= "2020-01-01" and x[
                    'lastUpdateTime'] >= "2020-01-01"]

    n1 = len(app_data)
    app_data = [x for x in app_data if not (x['firstInstallTime'] == x['lastUpdateTime']
                                            and (apply_time_int - cal_strdate_to_int(
                x['firstInstallTime'])) / 3600 / 1000 / 24 / 365 >= 1)]
    n2 = len(app_data)
    app_data = [x for x in app_data if
                not re.search(app_del_key.lower(), x['appName'].lower() + x['packageName'].lower())]
    if len(app_data) <= 0:
        print("app缺失", order_id)


    app_id = [x['packageName'] for x in app_data]
    first_install_time = [cal_strdate_to_int(x['firstInstallTime']) for x in app_data]
    last_update_time = [cal_strdate_to_int(x['lastUpdateTime']) for x in app_data]

    in_hours = [(apply_time_int - x) / 3600 / 1000 / 24 + random.random() * 0.0001 for x in first_install_time]
    up_hours = [(apply_time_int - x) / 3600 / 1000 / 24 + random.random() * 0.0001 for x in last_update_time]
    all_hours = [(y - x) / 3600 / 1000 / 24 + random.random() * 0.0001 for x, y in
                 zip(first_install_time, last_update_time)]

    in_hours_int = [int(x) for x in in_hours]
    app_inday_majority = Counter(in_hours_int).most_common(1)[0][0]

    """ in """
    app_name_and_id = [x['appName'] + ' ' + x['packageName'] + "@ xw " + str(
        (apply_time_int - cal_strdate_to_int(x['firstInstallTime'])) / 3600 / 1000 / 24 + random.random() * 0.0001) for
                       x in app_data]
    app_1d = [app_name_and_id[in_hours.index(x)] for x in in_hours if x <= 1]
    app_3d = [app_name_and_id[in_hours.index(x)] for x in in_hours if x <= 3]
    app_7d = [app_name_and_id[in_hours.index(x)] for x in in_hours if x <= 7]
    app_15d = [app_name_and_id[in_hours.index(x)] for x in in_hours if x <= 15]
    app_30d = [app_name_and_id[in_hours.index(x)] for x in in_hours if x <= 30]
    app_60d = [app_name_and_id[in_hours.index(x)] for x in in_hours if x <= 60]
    app_90d = [app_name_and_id[in_hours.index(x)] for x in in_hours if x <= 90]
    app_180d = [app_name_and_id[in_hours.index(x)] for x in in_hours if x <= 180]
    app_360d = [app_name_and_id[in_hours.index(x)] for x in in_hours if x <= 360]

    app_name_and_id_gg = [x['packageName'] for x in app_data]
    app_1d_gg = [app_name_and_id_gg[in_hours.index(x)] for x in in_hours if x <= 1]
    app_3d_gg = [app_name_and_id_gg[in_hours.index(x)] for x in in_hours if x <= 3]
    app_7d_gg = [app_name_and_id_gg[in_hours.index(x)] for x in in_hours if x <= 7]
    app_15d_gg = [app_name_and_id_gg[in_hours.index(x)] for x in in_hours if x <= 15]
    app_30d_gg = [app_name_and_id_gg[in_hours.index(x)] for x in in_hours if x <= 30]
    app_60d_gg = [app_name_and_id_gg[in_hours.index(x)] for x in in_hours if x <= 60]
    app_90d_gg = [app_name_and_id_gg[in_hours.index(x)] for x in in_hours if x <= 90]
    app_180d_gg = [app_name_and_id_gg[in_hours.index(x)] for x in in_hours if x <= 180]
    app_360d_gg = [app_name_and_id_gg[in_hours.index(x)] for x in in_hours if x <= 360]
    app_cnt = len(app_name_and_id)
    app_1d_cnt = len(app_1d)
    app_3d_cnt = len(app_3d)
    app_7d_cnt = len(app_7d)
    app_15d_cnt = len(app_15d)
    app_30d_cnt = len(app_30d)
    app_60d_cnt = len(app_60d)
    app_90d_cnt = len(app_90d)
    app_180d_cnt = len(app_180d)
    app_360d_cnt = len(app_360d)

    app_7d_rate_inday =      tmp_d(app_7d_cnt, app_cnt)
    app_cash_46_30d = [x for x in app_30d_gg if re.search(package_id_46, x + "@")]
    app_cash_46_30d_cnt = len(app_cash_46_30d)
    app_cash_46_360d = [x for x in app_360d_gg if re.search(package_id_46, x + "@")]
    app_cash_46_360d_cnt = len(app_cash_46_360d)
    app_cash_30d_div_360d_46_inday = tmp_d(app_cash_46_30d_cnt, app_cash_46_360d_cnt)
    app_cash_3d = [x for x in app_3d if re.search(package_id_loan, x)]
    app_cash_3d_cnt = len(app_cash_3d)
    app_cash_30d = [x for x in app_30d if re.search(package_id_loan, x)]
    app_cash_30d_cnt = len(app_cash_30d)
    app_cash_3d_div_30d_inday = tmp_d(app_cash_3d_cnt, app_cash_30d_cnt)
    app_1d_div_30d_inday = tmp_d(app_1d_cnt, app_30d_cnt)
    app_cash_42_7d = [x for x in app_7d_gg if re.search(package_id_42, x + "@")]
    app_cash_42_7d_cnt = len(app_cash_42_7d)
    app_cash_42_30d = [x for x in app_30d_gg if re.search(package_id_42, x + "@")]
    app_cash_42_30d_cnt = len(app_cash_42_30d)
    app_cash_7d_div_30d_42_inday = tmp_d(app_cash_42_7d_cnt, app_cash_42_30d_cnt)
    app_cash_2m_3d = [x for x in app_3d_gg if re.search(package_id_2m, x + "@")]
    app_cash_2m_3d_cnt = len(app_cash_2m_3d)
    app_cash_2m_30d = [x for x in app_30d_gg if re.search(package_id_2m, x + "@")]
    app_cash_2m_30d_cnt = len(app_cash_2m_30d)
    app_cash_3d_div_30d_2m_inday = tmp_d(app_cash_2m_3d_cnt, app_cash_2m_30d_cnt)
    app_cash_2m_90d = [x for x in app_90d_gg if re.search(package_id_2m, x + "@")]
    app_cash_2m_90d_cnt = len(app_cash_2m_90d)
    app_cash_30d_div_90d_2m_inday = tmp_d(app_cash_2m_30d_cnt, app_cash_2m_90d_cnt)
    app_cash_50w_7d = [x for x in app_7d_gg if re.search(package_id_50w, x + "@")]
    app_cash_50w_7d_cnt = len(app_cash_50w_7d)
    app_cash_50w_15d = [x for x in app_15d_gg if re.search(package_id_50w, x + "@")]
    app_cash_50w_15d_cnt = len(app_cash_50w_15d)
    app_cash_7d_div_15d_50w_inday = tmp_d(app_cash_50w_7d_cnt, app_cash_50w_15d_cnt)




    """ all """
    app_name_and_id = [x['appName'] + ' ' + x['packageName'] + "@ xw " + str(
        (cal_strdate_to_int(x['lastUpdateTime']) - cal_strdate_to_int(
            x['firstInstallTime'])) // 3600 / 1000 / 24 + random.random() * 0.0001) for x in
                       app_data]
    app_1d = [app_name_and_id[all_hours.index(x)] for x in all_hours if x <= 1]
    app_3d = [app_name_and_id[all_hours.index(x)] for x in all_hours if x <= 3]
    app_7d = [app_name_and_id[all_hours.index(x)] for x in all_hours if x <= 7]
    app_15d = [app_name_and_id[all_hours.index(x)] for x in all_hours if x <= 15]
    app_30d = [app_name_and_id[all_hours.index(x)] for x in all_hours if x <= 30]
    app_60d = [app_name_and_id[all_hours.index(x)] for x in all_hours if x <= 60]
    app_90d = [app_name_and_id[all_hours.index(x)] for x in all_hours if x <= 90]
    app_180d = [app_name_and_id[all_hours.index(x)] for x in all_hours if x <= 180]
    app_360d = [app_name_and_id[all_hours.index(x)] for x in all_hours if x <= 360]

    app_name_and_id_gg = [x['packageName'] for x in app_data]
    app_1d_gg = [app_name_and_id_gg[all_hours.index(x)] for x in all_hours if x <= 1]
    app_3d_gg = [app_name_and_id_gg[all_hours.index(x)] for x in all_hours if x <= 3]
    app_7d_gg = [app_name_and_id_gg[all_hours.index(x)] for x in all_hours if x <= 7]
    app_15d_gg = [app_name_and_id_gg[all_hours.index(x)] for x in all_hours if x <= 15]
    app_30d_gg = [app_name_and_id_gg[all_hours.index(x)] for x in all_hours if x <= 30]
    app_60d_gg = [app_name_and_id_gg[all_hours.index(x)] for x in all_hours if x <= 60]
    app_90d_gg = [app_name_and_id_gg[all_hours.index(x)] for x in all_hours if x <= 90]
    app_180d_gg = [app_name_and_id_gg[all_hours.index(x)] for x in all_hours if x <= 180]
    app_360d_gg = [app_name_and_id_gg[all_hours.index(x)] for x in all_hours if x <= 360]

    app_cnt = len(app_name_and_id)
    app_1d_cnt = len(app_1d)
    app_3d_cnt = len(app_3d)
    app_7d_cnt = len(app_7d)
    app_15d_cnt = len(app_15d)
    app_30d_cnt = len(app_30d)
    app_60d_cnt = len(app_60d)
    app_90d_cnt = len(app_90d)
    app_180d_cnt = len(app_180d)
    app_360d_cnt = len(app_360d)

    app_cash_50w_1d = [x for x in app_1d_gg if re.search(package_id_50w, x + "@")]
    app_cash_50w_1d_cnt = len(app_cash_50w_1d)
    app_cash_50w_30d = [x for x in app_30d_gg if re.search(package_id_50w, x + "@")]
    app_cash_50w_30d_cnt = len(app_cash_50w_30d)
    app_cash_1d_div_30d_50w_allday = tmp_d(app_cash_50w_1d_cnt, app_cash_50w_30d_cnt)
    app_cash_2m_90d = [x for x in app_90d_gg if re.search(package_id_2m, x + "@")]
    app_cash_2m_90d_cnt = len(app_cash_2m_90d)
    app_cash_2m_180d = [x for x in app_180d_gg if re.search(package_id_2m, x + "@")]
    app_cash_2m_180d_cnt = len(app_cash_2m_180d)
    app_cash_90d_div_180d_2m_allday = tmp_d(app_cash_2m_90d_cnt, app_cash_2m_180d_cnt)
    app_cash_50w_1d = [x for x in app_1d_gg if re.search(package_id_50w, x + "@")]
    app_cash_50w_1d_cnt = len(app_cash_50w_1d)
    app_cash_50w_90d = [x for x in app_90d_gg if re.search(package_id_50w, x + "@")]
    app_cash_50w_90d_cnt = len(app_cash_50w_90d)
    app_cash_1d_div_90d_50w_allday = tmp_d(app_cash_50w_1d_cnt, app_cash_50w_90d_cnt)



    """ up """
    app_name_and_id = [x['appName'] + ' ' + x['packageName'] + "@ xw " + str(
        (apply_time_int - cal_strdate_to_int(x['lastUpdateTime'])) / 3600 / 1000 / 24 + random.random() * 0.0001) for x
                       in app_data]
    app_1d = [app_name_and_id[up_hours.index(x)] for x in up_hours if x <= 1]
    app_3d = [app_name_and_id[up_hours.index(x)] for x in up_hours if x <= 3]
    app_7d = [app_name_and_id[up_hours.index(x)] for x in up_hours if x <= 7]
    app_15d = [app_name_and_id[up_hours.index(x)] for x in up_hours if x <= 15]
    app_30d = [app_name_and_id[up_hours.index(x)] for x in up_hours if x <= 30]
    app_60d = [app_name_and_id[up_hours.index(x)] for x in up_hours if x <= 60]
    app_90d = [app_name_and_id[up_hours.index(x)] for x in up_hours if x <= 90]
    app_180d = [app_name_and_id[up_hours.index(x)] for x in up_hours if x <= 180]
    app_360d = [app_name_and_id[up_hours.index(x)] for x in up_hours if x <= 360]

    app_cnt = len(app_name_and_id)
    app_1d_cnt = len(app_1d)
    app_3d_cnt = len(app_3d)
    app_7d_cnt = len(app_7d)
    app_15d_cnt = len(app_15d)
    app_30d_cnt = len(app_30d)
    app_60d_cnt = len(app_60d)
    app_90d_cnt = len(app_90d)
    app_180d_cnt = len(app_180d)
    app_360d_cnt = len(app_360d)

    app_name_and_id_gg = [x['packageName'] for x in app_data]
    app_1d_gg = [app_name_and_id_gg[up_hours.index(x)] for x in up_hours if x <= 1]
    app_3d_gg = [app_name_and_id_gg[up_hours.index(x)] for x in up_hours if x <= 3]
    app_7d_gg = [app_name_and_id_gg[up_hours.index(x)] for x in up_hours if x <= 7]
    app_15d_gg = [app_name_and_id_gg[up_hours.index(x)] for x in up_hours if x <= 15]
    app_30d_gg = [app_name_and_id_gg[up_hours.index(x)] for x in up_hours if x <= 30]
    app_60d_gg = [app_name_and_id_gg[up_hours.index(x)] for x in up_hours if x <= 60]
    app_90d_gg = [app_name_and_id_gg[up_hours.index(x)] for x in up_hours if x <= 90]
    app_180d_gg = [app_name_and_id_gg[up_hours.index(x)] for x in up_hours if x <= 180]
    app_360d_gg = [app_name_and_id_gg[up_hours.index(x)] for x in up_hours if x <= 360]



    in_hours_int = [int(x) for x in in_hours]
    app_inday_majority_upday = Counter(in_hours_int).most_common(1)[0][0]
    app_cash_30d = [x for x in app_30d if re.search(package_id_loan, x)]
    app_cash_30d_cnt = len(app_cash_30d)
    app_cash_180d = [x for x in app_180d if re.search(package_id_loan, x)]
    app_cash_180d_cnt = len(app_cash_180d)
    app_cash_30d_div_180d_upday = tmp_d(app_cash_30d_cnt, app_cash_180d_cnt)

    app_cash_1m_180d = [x for x in app_180d_gg if re.search(package_id_1m, x + "@")]
    app_cash_1m_180d_cnt = len(app_cash_1m_180d)
    app_cash = [x for x in app_name_and_id if re.search(package_id_loan, x)]
    app_cash_cnt = len(app_cash)
    app_cash_1m_180d_rate_upday = tmp_d(app_cash_1m_180d_cnt, app_cash_cnt)
    app_cash_3d = [x for x in app_3d if re.search(package_id_loan, x)]
    app_cash_3d_cnt = len(app_cash_3d)
    app_cash_3d_rate_upday = tmp_d(app_cash_3d_cnt, app_3d_cnt)
    app_cash_7d = [x for x in app_7d if re.search(package_id_loan, x)]
    app_cash_7d_cnt = len(app_cash_7d)
    app_cash_7d_rate_upday = tmp_d(app_cash_7d_cnt, app_7d_cnt)
    app_cash_48_3d = [x for x in app_3d_gg if re.search(package_id_48, x + "@")]
    app_cash_48_3d_cnt = len(app_cash_48_3d)
    app_cash_48_7d = [x for x in app_7d_gg if re.search(package_id_48, x + "@")]
    app_cash_48_7d_cnt = len(app_cash_48_7d)
    app_cash_3d_div_7d_48_upday = tmp_d(app_cash_48_3d_cnt, app_cash_48_7d_cnt)

    """3 sms模块特征工程"""
    """3.1 tfidf 角度"""
    if isinstance(sms_json, str):
        sms_json = json.loads(sms_json)
    sms_json = [x for x in sms_json if "sms_content" in x.keys()]
    sms_json = [x for x in sms_json if x['sms_content'] and x['send_time'] and x['contactor_name']]
    sms_json = [x for x in sms_json if x['send_time'] >= '2019-10-10 00:00:00']  # 42959038-03-26 03:03:43

    sms_json = [{**x, "day": (apply_time_int - cal_strdate_to_int(x["send_time"])) / 3600 / 1000 / 24} for x in sms_json]
    sms_json = [ x for x in sms_json if x["day"]<=180]


    sms_json = [x for x in sms_json if
                not re.search(sms_dele_key, unidecode.unidecode(x['sms_content'].lower()))]  # 42959038-03-26 03:03:43
    sms_json = [x for x in sms_json if not re.search(sms_dele_key, unidecode.unidecode(
        x['contactor_name'].lower()))]  # 42959038-03-26 03:03:43

    body = [item['sms_content'].replace('\n', '').lower() + " " + item['contactor_name'].replace('\n', '').lower() for
            item in sms_json]  # 删除换行符
    body = [cal_sms_content(unidecode.unidecode(x.lower())) for x in body]  # 去掉重音符

    send_time = [item['send_time'] for item in sms_json]
    send_time = [cal_strdate_to_int(item['send_time']) for item in sms_json]
    day = [(apply_time_int - x) / 3600 / 1000 / 24 + random.random() * 0.0001 for x in send_time]

    body_1d = [body[day.index(x)] for x in day if x <= 1]
    body_3d = [body[day.index(x)] for x in day if x <= 3]
    body_7d = [body[day.index(x)] for x in day if x <= 7]
    body_15d = [body[day.index(x)] for x in day if x <= 15]
    body_30d = [body[day.index(x)] for x in day if x <= 30]
    body_60d = [body[day.index(x)] for x in day if x <= 60]
    body_90d = [body[day.index(x)] for x in day if x <= 90]
    body_360d = [body[day.index(x)] for x in day if x <= 360]

    body_cnt = len(body)
    body_1d_cnt = len(body_1d)
    body_1d_rate = tmp_d(body_1d_cnt, body_cnt)
    body_3d_cnt = len(body_3d)
    body_3d_rate = tmp_d(body_3d_cnt, body_cnt)
    body_7d_cnt = len(body_7d)
    body_7d_rate = tmp_d(body_7d_cnt, body_cnt)
    body_15d_cnt = len(body_15d)
    body_15d_rate = tmp_d(body_15d_cnt, body_cnt)
    body_30d_cnt = len(body_30d)
    body_30d_rate = tmp_d(body_30d_cnt, body_cnt)
    body_60d_cnt = len(body_60d)
    body_60d_rate = tmp_d(body_60d_cnt, body_cnt)
    body_90d_cnt = len(body_90d)
    body_90d_rate = tmp_d(body_90d_cnt, body_cnt)
    body_360d_cnt = len(body_360d)
    body_360d_rate = tmp_d(body_360d_cnt, body_cnt)

    body_1d_upto = [x for x in body_1d if re.search('approve|up to|approval|interest|upto', x)]
    body_1d_upto_cnt = len(body_1d_upto)
    body_1d_upto_rate_tfidf = tmp_d(body_1d_upto_cnt, body_1d_cnt)
    body_7d_duedate = [x for x in body_7d if re.search(' duedate | dueday | ay due |due date', x.lower())]
    body_7d_duedate_cnt = len(body_7d_duedate)
    body_7d_duedate_rate = tmp_d(body_7d_duedate_cnt, body_7d_cnt)
    body_duedate = [x for x in body if re.search(' duedate | dueday | ay due |due date', x.lower())]
    body_duedate_cnt = len(body_duedate)
    body_7d_duedate_div_duedate_tfidf = tmp_d(body_7d_duedate_cnt, body_duedate_cnt)
    body_15d_congrat = [x for x in body_15d if re.search('congrat', x)]
    body_15d_congrat_cnt = len(body_15d_congrat)
    body_60d_congrat = [x for x in body_60d if re.search('congrat', x)]
    body_60d_congrat_cnt = len(body_60d_congrat)
    body_congrat_15d_div_60d_tfidf = tmp_d(body_15d_congrat_cnt, body_60d_congrat_cnt)
    body_30d_advance = [x for x in body_30d if re.search("advance", x.lower())]
    body_30d_advance_cnt = len(body_30d_advance)
    body_360d_advance = [x for x in body_360d if re.search("advance", x.lower())]
    body_360d_advance_cnt = len(body_360d_advance)
    body_advance_30d_div_360d_tfidf = tmp_d(body_30d_advance_cnt, body_360d_advance_cnt)
    body_15d_upto = [x for x in body_15d if re.search('approve|up to|approval|interest|upto', x)]
    body_15d_upto_cnt = len(body_15d_upto)
    body_360d_upto = [x for x in body_360d if re.search('approve|up to|approval|interest|upto', x)]
    body_360d_upto_cnt = len(body_360d_upto)
    body_upto_15d_div_360d_tfidf = tmp_d(body_15d_upto_cnt, body_360d_upto_cnt)
    body_3d_discount = [x for x in body_3d if re.search('discount', x)]
    body_3d_discount_cnt = len(body_3d_discount)
    body_30d_discount = [x for x in body_30d if re.search('discount', x)]
    body_30d_discount_cnt = len(body_30d_discount)
    body_discount_3d_div_30d_tfidf = tmp_d(body_3d_discount_cnt, body_30d_discount_cnt)
    body_15d_close = [x for x in body_15d if re.search('close', x)]
    body_15d_close_cnt = len(body_15d_close)
    body_60d_close = [x for x in body_60d if re.search('close', x)]
    body_60d_close_cnt = len(body_60d_close)
    body_close_15d_div_60d_tfidf = tmp_d(body_15d_close_cnt, body_60d_close_cnt)
    body_90d_legal = [x for x in body_90d if re.search(' legal | law | atty ', x.lower())]
    body_90d_legal_cnt_tfidf = len(body_90d_legal)
    body_90d_discount = [x for x in body_90d if re.search('discount', x)]
    body_90d_discount_cnt = len(body_90d_discount)
    body_90d_discount_rate = tmp_d(body_90d_discount_cnt, body_90d_cnt)
    body_discount = [x for x in body if re.search('discount', x)]
    body_discount_cnt = len(body_discount)
    body_90d_discount_div_discount_tfidf = tmp_d(body_90d_discount_cnt, body_discount_cnt)
    body_3d_pastdue = [x for x in body_3d if re.search('past due|pastdue|over due|overdue', x.lower())]
    body_3d_pastdue_cnt = len(body_3d_pastdue)
    body_3d_pastdue_rate = tmp_d(body_3d_pastdue_cnt, body_3d_cnt)
    body_pastdue = [x for x in body if re.search('past due|pastdue|over due|overdue', x.lower())]
    body_pastdue_cnt = len(body_pastdue)
    body_3d_pastdue_div_pastdue_tfidf = tmp_d(body_3d_pastdue_cnt, body_pastdue_cnt)
    body_60d_collection = [x for x in body_60d if re.search("collection", x.lower())]
    body_60d_collection_cnt = len(body_60d_collection)
    body_60d_collection_rate = tmp_d(body_60d_collection_cnt, body_60d_cnt)
    body_collection = [x for x in body if re.search("collection", x.lower())]
    body_collection_cnt = len(body_collection)
    body_60d_collection_div_collection_tfidf = tmp_d(body_60d_collection_cnt, body_collection_cnt)
    body_360d_paid = [x for x in body_360d if re.search(" paid | received | posted | settled ", x.lower())]
    body_360d_paid_cnt_tfidf = len(body_360d_paid)
    body_1d_now = [x for x in body_1d if re.search("today|now|tomorrow", x.lower())]
    body_1d_now_cnt = len(body_1d_now)
    body_7d_now = [x for x in body_7d if re.search("today|now|tomorrow", x.lower())]
    body_7d_now_cnt = len(body_7d_now)
    body_now_1d_div_7d_tfidf = tmp_d(body_1d_now_cnt, body_7d_now_cnt)
    body_15d_pastdue = [x for x in body_15d if re.search('past due|pastdue|over due|overdue', x.lower())]
    body_15d_pastdue_cnt = len(body_15d_pastdue)
    body_90d_pastdue = [x for x in body_90d if re.search('past due|pastdue|over due|overdue', x.lower())]
    body_90d_pastdue_cnt = len(body_90d_pastdue)
    body_pastdue_15d_div_90d_tfidf = tmp_d(body_15d_pastdue_cnt, body_90d_pastdue_cnt)
    body_3d_https = [x for x in body_3d if re.search("https", x.lower())]
    body_3d_https_cnt = len(body_3d_https)
    body_3d_https_rate = tmp_d(body_3d_https_cnt, body_3d_cnt)
    body_https = [x for x in body if re.search("https", x.lower())]
    body_https_cnt = len(body_https)
    body_3d_https_div_https_tfidf = tmp_d(body_3d_https_cnt, body_https_cnt)
    body_1d_paid = [x for x in body_1d if re.search(" paid | received | posted | settled ", x.lower())]
    body_1d_paid_cnt = len(body_1d_paid)
    body_3d_paid = [x for x in body_3d if re.search(" paid | received | posted | settled ", x.lower())]
    body_3d_paid_cnt = len(body_3d_paid)
    body_paid_1d_div_3d_tfidf = tmp_d(body_1d_paid_cnt, body_3d_paid_cnt)
    body_60d_https = [x for x in body_60d if re.search("https", x.lower())]
    body_60d_https_cnt = len(body_60d_https)
    body_90d_https = [x for x in body_90d if re.search("https", x.lower())]
    body_90d_https_cnt = len(body_90d_https)
    body_https_60d_div_90d_tfidf = tmp_d(body_60d_https_cnt, body_90d_https_cnt)
    body_30d_https = [x for x in body_30d if re.search("https", x.lower())]
    body_30d_https_cnt = len(body_30d_https)
    body_https_3d_div_30d_tfidf = tmp_d(body_3d_https_cnt, body_30d_https_cnt)
    body_3d_congrat = [x for x in body_3d if re.search('congrat', x)]
    body_3d_congrat_cnt = len(body_3d_congrat)
    body_congrat_3d_div_15d_tfidf = tmp_d(body_3d_congrat_cnt, body_15d_congrat_cnt)
    body_3d_now = [x for x in body_3d if re.search("today|now|tomorrow", x.lower())]
    body_3d_now_cnt = len(body_3d_now)
    body_15d_now = [x for x in body_15d if re.search("today|now|tomorrow", x.lower())]
    body_15d_now_cnt = len(body_15d_now)
    body_now_3d_div_15d_tfidf = tmp_d(body_3d_now_cnt, body_15d_now_cnt)
    body_7d_congrat = [x for x in body_7d if re.search('congrat', x)]
    body_7d_congrat_cnt = len(body_7d_congrat)
    body_congrat_3d_div_7d_tfidf = tmp_d(body_3d_congrat_cnt, body_7d_congrat_cnt)
    body_360d_duedate = [x for x in body_360d if re.search(' duedate | dueday | ay due |due date', x.lower())]
    body_360d_duedate_cnt = len(body_360d_duedate)
    body_360d_duedate_div_duedate = tmp_d(body_360d_duedate_cnt, body_duedate_cnt)
    body_duedate_cnt_tfidf = body_duedate_cnt
    body_close = [x for x in body if re.search('close', x)]
    body_close_cnt = len(body_close)
    body_15d_close_div_close_tfidf = tmp_d(body_15d_close_cnt, body_close_cnt)
    body_360d_paid = [x for x in body_360d if re.search(" paid | received | posted | settled ", x.lower())]
    body_360d_paid_cnt = len(body_360d_paid)
    body_360d_paid_rate = tmp_d(body_360d_paid_cnt, body_360d_cnt)
    body_paid = [x for x in body if re.search(" paid | received | posted | settled ", x.lower())]
    body_paid_cnt = len(body_paid)
    body_360d_paid_div_paid_tfidf = tmp_d(body_360d_paid_cnt, body_paid_cnt)
    body_30d_pastdue = [x for x in body_30d if re.search('past due|pastdue|over due|overdue', x.lower())]
    body_30d_pastdue_cnt = len(body_30d_pastdue)
    body_pastdue_3d_div_30d_tfidf = tmp_d(body_3d_pastdue_cnt, body_30d_pastdue_cnt)
    body_3d_reloan = [x for x in body_3d if re.search("reloan| re loan ", x.lower())]
    body_3d_reloan_cnt = len(body_3d_reloan)
    body_7d_reloan = [x for x in body_7d if re.search("reloan| re loan ", x.lower())]
    body_7d_reloan_cnt = len(body_7d_reloan)
    body_reloan_3d_div_7d_tfidf = tmp_d(body_3d_reloan_cnt, body_7d_reloan_cnt)
    body_15d_apply = [x for x in body_15d if re.search("application|apply", x.lower())]
    body_15d_apply_cnt = len(body_15d_apply)
    body_30d_apply = [x for x in body_30d if re.search("application|apply", x.lower())]
    body_30d_apply_cnt = len(body_30d_apply)
    body_apply_15d_div_30d_tfidf = tmp_d(body_15d_apply_cnt, body_30d_apply_cnt)

    """ 3.2 org模块特征工程 """
    if isinstance(sms_json, str):
        sms_json = json.loads(sms_json)

    sms_json = [x for x in sms_json if "sms_content" in x.keys()]
    sms_json = [x for x in sms_json if x['sms_content'] and x['send_time'] and x['contactor_name']]
    sms_json = [x for x in sms_json if x['send_time'] >= '2019-10-10 00:00:00']  # 42959038-03-26 03:03:43

    sms_json = [{**x, "day": (apply_time_int - cal_strdate_to_int(x["send_time"])) / 3600 / 1000 / 24} for x in sms_json]
    sms_json = [ x for x in sms_json if x["day"]<=180]


    sms_json = [x for x in sms_json if not re.search(sms_dele_key, unidecode.unidecode(x['sms_content'].lower()))]
    sms_json = [x for x in sms_json if not re.search(sms_dele_key, unidecode.unidecode(x['contactor_name'].lower()))]

    sms_json = [
        {**x, 'sms_org_ok': cal_sms_org_all(sms_org_list, sms_org_pattern, x["sms_content"], x["contactor_name"])} for x
        in sms_json]
    sms_json = [{**x, "sms_org_bank": regular_extraction(bank_org_pattern, x["sms_content"] + x["contactor_name"])} for
                x in sms_json]

    sms_json = [{**x, "sms_content_ok": cal_sms_content(x["sms_content"])} for x in sms_json]
    sms_json = [{**x, "sms_verif_key": cal_boy_type(x["sms_content_ok"], sms_verif_key)} for x in sms_json]
    sms_json = [{**x, "sms_cash_key": cal_boy_type(x["sms_content_ok"], sms_cash_key)} for x in sms_json]
    sms_json = [{**x, "sms_loan_key": cal_boy_type(x["sms_content_ok"], sms_loan_key)} for x in sms_json]
    sms_json = [{**x, "sms_repayment_key": cal_boy_type(x["sms_content_ok"], sms_repayment_key)} for x in sms_json]
    sms_json = [{**x, "sms_expire_key": cal_boy_type(cal_regular_simple(x["sms_content_ok"]),
                                                     cal_regular_simple(sms_expire_key))} for x in sms_json]
    sms_json = [{**x, "sms_bill_key": cal_boy_type(x["sms_content_ok"], sms_bill_key)} for x in sms_json]
    sms_json = [{**x, "sms_overdue_serious_key": cal_boy_type(x["sms_content_ok"], sms_overdue_serious_key)} for x in
                sms_json]
    sms_json = [
        {**x, "sms_overdue_key": cal_boy_type(x["sms_content_ok"], sms_overdue_key + "|" + sms_overdue_serious_key)} for
        x in sms_json]
    sms_json = [{**x, "sms_topcash_key": cal_boy_type(x["sms_org_ok"], sms_topcash_key)} for x in sms_json]
    sms_json = [{**x, "sms_1m_cash_key": cal_boy_type(x["sms_org_ok"], sms_1m_cash_key)} for x in sms_json]

    sms_json = [{**x, "day": (apply_time_int - cal_strdate_to_int(x["send_time"])) / 3600 / 1000 / 24} for x in
                sms_json]

    body = [x for x in sms_json]
    body_1d = [x for x in sms_json if x["day"] <= 1]
    body_3d = [x for x in sms_json if x["day"] <= 3]
    body_7d = [x for x in sms_json if x["day"] <= 7]
    body_15d = [x for x in sms_json if x["day"] <= 15]
    body_30d = [x for x in sms_json if x["day"] <= 30]
    body_60d = [x for x in sms_json if x["day"] <= 60]
    body_90d = [x for x in sms_json if x["day"] <= 90]
    body_360d = [x for x in sms_json if x["day"] <= 360]

    tmp = [x['sms_org_ok'] for x in body if x['sms_org_ok'] != "-999"]
    body_cnt = len(set(tmp))
    tmp = [x['sms_org_ok'] for x in body_1d if x['sms_org_ok'] != "-999"]
    body_1d_cnt = len(set(tmp))
    body_1d_rate = tmp_d(body_1d_cnt, body_cnt)
    tmp = [x['sms_org_ok'] for x in body_3d if x['sms_org_ok'] != "-999"]
    body_3d_cnt = len(set(tmp))
    body_3d_rate = tmp_d(body_3d_cnt, body_cnt)
    tmp = [x['sms_org_ok'] for x in body_7d if x['sms_org_ok'] != "-999"]
    body_7d_cnt = len(set(tmp))
    body_7d_rate = tmp_d(body_7d_cnt, body_cnt)
    tmp = [x['sms_org_ok'] for x in body_15d if x['sms_org_ok'] != "-999"]
    body_15d_cnt = len(set(tmp))
    body_15d_rate = tmp_d(body_15d_cnt, body_cnt)
    tmp = [x['sms_org_ok'] for x in body_30d if x['sms_org_ok'] != "-999"]
    body_30d_cnt = len(set(tmp))
    body_30d_rate = tmp_d(body_30d_cnt, body_cnt)
    tmp = [x['sms_org_ok'] for x in body_60d if x['sms_org_ok'] != "-999"]
    body_60d_cnt = len(set(tmp))
    body_60d_rate = tmp_d(body_60d_cnt, body_cnt)
    tmp = [x['sms_org_ok'] for x in body_90d if x['sms_org_ok'] != "-999"]
    body_90d_cnt = len(set(tmp))
    body_90d_rate = tmp_d(body_90d_cnt, body_cnt)
    tmp = [x['sms_org_ok'] for x in body_360d if x['sms_org_ok'] != "-999"]
    body_360d_cnt = len(set(tmp))
    body_360d_rate = tmp_d(body_360d_cnt, body_cnt)

    body_7d_overdue = [x['sms_org_ok'] for x in sms_json if x["sms_overdue_key"] == 1 and x['sms_loan_key'] == 0
                       and x['sms_repayment_key'] == 0 and x['sms_expire_key'] == 0 and x["day"] <= 7 and x[
                           'sms_org_ok'] != "-999"]
    body_7d_overdue = set(body_7d_overdue)
    body_7d_overdue_cnt = len(body_7d_overdue)
    body_7d_overdue_rate_org = tmp_d(body_7d_overdue_cnt, body_7d_cnt)
    body_360d_cash_verif = [x['sms_org_ok'] for x in sms_json if
                            x["sms_verif_key"] == 1 and x["sms_cash_key"] == 1 and x["day"] <= 360 and x[
                                'sms_org_ok'] != "-999"]
    body_360d_cash_verif = set(body_360d_cash_verif)
    body_360d_cash_verif_cnt = len(body_360d_cash_verif)
    body_360d_cash_verif_rate_org = tmp_d(body_360d_cash_verif_cnt, body_360d_cnt)
    body_15d_verif = [x['sms_org_ok'] for x in sms_json if
                      x["sms_verif_key"] == 1 and x["day"] <= 15 and x['sms_org_ok'] != "-999"]
    body_15d_verif = set(body_15d_verif)
    body_15d_verif_cnt = len(body_15d_verif)
    body_15d_verif_rate_org = tmp_d(body_15d_verif_cnt, body_15d_cnt)
    body_7d_overdue = [x['sms_org_ok'] for x in sms_json if x["sms_overdue_key"] == 1 and x['sms_loan_key'] == 0
                       and x['sms_repayment_key'] == 0 and x['sms_expire_key'] == 0 and x["day"] <= 7 and x[
                           'sms_org_ok'] != "-999"]
    body_7d_overdue = set(body_7d_overdue)
    body_7d_overdue_cnt = len(body_7d_overdue)
    body_30d_overdue = [x['sms_org_ok'] for x in sms_json if x["sms_overdue_key"] == 1 and x['sms_loan_key'] == 0
                        and x['sms_repayment_key'] == 0 and x['sms_expire_key'] == 0 and x["day"] <= 30 and x[
                            'sms_org_ok'] != "-999"]
    body_30d_overdue = set(body_30d_overdue)
    body_30d_overdue_cnt = len(body_30d_overdue)
    body_overdue_7d_div_30d_org = tmp_d(body_7d_overdue_cnt, body_30d_overdue_cnt)
    body_60d_cash_verif = [x['sms_org_ok'] for x in sms_json if
                           x["sms_verif_key"] == 1 and x["sms_cash_key"] == 1 and x["day"] <= 60 and x[
                               'sms_org_ok'] != "-999"]
    body_60d_cash_verif = set(body_60d_cash_verif)
    body_60d_cash_verif_cnt = len(body_60d_cash_verif)
    body_60d_cash_verif_rate_org = tmp_d(body_60d_cash_verif_cnt, body_60d_cnt)
    body_3d_overdue_1m_cash = [x['sms_org_ok'] for x in sms_json if
                               x["sms_overdue_key"] == 1 and x['sms_loan_key'] == 0 and x['sms_1m_cash_key'] == 1
                               and x['sms_repayment_key'] == 0 and x['sms_expire_key'] == 0 and x["day"] <= 3 and
                               x['sms_org_ok'] != "-999"]
    body_3d_overdue_1m_cash = set(body_3d_overdue_1m_cash)
    body_3d_overdue_1m_cash_cnt = len(body_3d_overdue_1m_cash)
    body_7d_overdue_1m_cash = [x['sms_org_ok'] for x in sms_json if
                               x["sms_overdue_key"] == 1 and x['sms_loan_key'] == 0 and x['sms_1m_cash_key'] == 1
                               and x['sms_repayment_key'] == 0 and x['sms_expire_key'] == 0 and x["day"] <= 7 and
                               x['sms_org_ok'] != "-999"]
    body_7d_overdue_1m_cash = set(body_7d_overdue_1m_cash)
    body_7d_overdue_1m_cash_cnt = len(body_7d_overdue_1m_cash)
    body_overdue_1m_cash_3d_div_7d_org = tmp_d(body_3d_overdue_1m_cash_cnt, body_7d_overdue_1m_cash_cnt)
    body_7d_overdue_serious = [x['sms_org_ok'] for x in sms_json if
                               x["sms_overdue_serious_key"] == 1 and x["day"] <= 7 and x['sms_org_ok'] != "-999"]
    body_7d_overdue_serious = set(body_7d_overdue_serious)
    body_7d_overdue_serious_cnt = len(body_7d_overdue_serious)
    body_360d_overdue_serious = [x['sms_org_ok'] for x in sms_json if
                                 x["sms_overdue_serious_key"] == 1 and x["day"] <= 360 and x['sms_org_ok'] != "-999"]
    body_360d_overdue_serious = set(body_360d_overdue_serious)
    body_360d_overdue_serious_cnt = len(body_360d_overdue_serious)
    body_overdue_serious_7d_div_360d_org = tmp_d(body_7d_overdue_serious_cnt, body_360d_overdue_serious_cnt)
    body_3d_overdue_serious = [x['sms_org_ok'] for x in sms_json if
                               x["sms_overdue_serious_key"] == 1 and x["day"] <= 3 and x['sms_org_ok'] != "-999"]
    body_3d_overdue_serious = set(body_3d_overdue_serious)
    body_3d_overdue_serious_cnt = len(body_3d_overdue_serious)
    body_30d_overdue_serious = [x['sms_org_ok'] for x in sms_json if
                                x["sms_overdue_serious_key"] == 1 and x["day"] <= 30 and x['sms_org_ok'] != "-999"]
    body_30d_overdue_serious = set(body_30d_overdue_serious)
    body_30d_overdue_serious_cnt = len(body_30d_overdue_serious)
    body_overdue_serious_3d_div_30d_org = tmp_d(body_3d_overdue_serious_cnt, body_30d_overdue_serious_cnt)
    body_15d_expire = [x['sms_org_ok'] for x in sms_json if
                       x["sms_expire_key"] == 1 and x["day"] <= 15 and x[
                           'sms_org_ok'] != "-999"]
    body_15d_expire = set(body_15d_expire)
    body_15d_expire_cnt = len(body_15d_expire)
    body_360d_expire = [x['sms_org_ok'] for x in sms_json if
                        x["sms_expire_key"] == 1 and x["day"] <= 360 and x[
                            'sms_org_ok'] != "-999"]
    body_360d_expire = set(body_360d_expire)
    body_360d_expire_cnt = len(body_360d_expire)
    body_expire_15d_div_360d_org = tmp_d(body_15d_expire_cnt, body_360d_expire_cnt)

    body_7d_loan = [x['sms_org_ok'] for x in sms_json if
                    x["sms_loan_key"] == 1 and x["day"] <= 7 and x['sms_org_ok'] != "-999"]
    body_7d_loan = set(body_7d_loan)
    body_7d_loan_cnt = len(body_7d_loan)
    body_7d_loan_rate = tmp_d(body_7d_loan_cnt, body_7d_cnt)
    body_loan = [x['sms_org_ok'] for x in sms_json if x["sms_loan_key"] == 1 and x['sms_org_ok'] != "-999"]
    body_loan = set(body_loan)
    body_loan_cnt = len(body_loan)
    body_7d_loan_div_loan_org = tmp_d(body_7d_loan_cnt, body_loan_cnt)
    body_1d_overdue_topcash = [x['sms_org_ok'] for x in sms_json if
                               x["sms_overdue_key"] == 1 and x['sms_loan_key'] == 0 and x['sms_topcash_key'] == 1
                               and x['sms_repayment_key'] == 0 and x['sms_expire_key'] == 0 and x["day"] <= 1 and
                               x['sms_org_ok'] != "-999"]
    body_1d_overdue_topcash = set(body_1d_overdue_topcash)
    body_1d_overdue_topcash_cnt = len(body_1d_overdue_topcash)
    body_3d_overdue_topcash = [x['sms_org_ok'] for x in sms_json if
                               x["sms_overdue_key"] == 1 and x['sms_loan_key'] == 0 and x['sms_topcash_key'] == 1
                               and x['sms_repayment_key'] == 0 and x['sms_expire_key'] == 0 and x["day"] <= 3 and
                               x['sms_org_ok'] != "-999"]
    body_3d_overdue_topcash = set(body_3d_overdue_topcash)
    body_3d_overdue_topcash_cnt = len(body_3d_overdue_topcash)
    body_overdue_topcash_1d_div_3d_org = tmp_d(body_1d_overdue_topcash_cnt, body_3d_overdue_topcash_cnt)

    """ 3.3 sms基本信息模块 """
    if isinstance(sms_json, str):
        sms_json = json.loads(sms_json)
    sms_json = [x for x in sms_json if "sms_content" in x.keys()]
    sms_json = [x for x in sms_json if x['sms_content'] and x['send_time'] and x['contactor_name']]
    sms_json = [x for x in sms_json if x['send_time'] >= '2019-10-10 00:00:00']  # 42959038-03-26 03:03:43

    sms_json = [{**x, "day": (apply_time_int - cal_strdate_to_int(x["send_time"])) / 3600 / 1000 / 24} for x in sms_json]
    sms_json = [ x for x in sms_json if x["day"]<=180]


    sms_json = [x for x in sms_json if not re.search(sms_dele_key, unidecode.unidecode(x['sms_content'].lower()))]
    sms_json = [x for x in sms_json if not re.search(sms_dele_key, unidecode.unidecode(x['contactor_name'].lower()))]

    # 新逻辑
    sms_json = [
        {**x, 'sms_org_ok': cal_sms_org_all(sms_org_list, sms_org_pattern, x["sms_content"], x["contactor_name"])} for x
        in sms_json]
    sms_json = [{**x, "sms_org_bank": regular_extraction(bank_org_pattern, x["sms_content"] + x["contactor_name"])} for
                x in sms_json]

    sms_json = [{**x, "sms_content_ok": cal_sms_content(x["sms_content"])} for x in sms_json]
    sms_json = [{**x, "sms_verif_key": cal_boy_type(x["sms_content_ok"], sms_verif_key)} for x in sms_json]
    sms_json = [{**x, "sms_cash_key": cal_boy_type(x["sms_content_ok"], sms_cash_key)} for x in sms_json]
    sms_json = [{**x, "sms_loan_key": cal_boy_type(x["sms_content_ok"], sms_loan_key)} for x in sms_json]
    sms_json = [{**x, "sms_repayment_key": cal_boy_type(x["sms_content_ok"], sms_repayment_key)} for x in sms_json]
    sms_json = [{**x, "sms_expire_key": cal_boy_type(cal_regular_simple(x["sms_content_ok"]),
                                                     cal_regular_simple(sms_expire_key))} for x in sms_json]
    sms_json = [{**x, "sms_bill_key": cal_boy_type(x["sms_content_ok"], sms_bill_key)} for x in sms_json]
    sms_json = [{**x, "sms_overdue_serious_key": cal_boy_type(x["sms_content_ok"], sms_overdue_serious_key)} for x in
                sms_json]
    sms_json = [
        {**x, "sms_overdue_key": cal_boy_type(x["sms_content_ok"], sms_overdue_key + "|" + sms_overdue_serious_key)} for
        x in sms_json]
    sms_json = [{**x, "sms_topcash_key": cal_boy_type(x["sms_org_ok"], sms_topcash_key)} for x in sms_json]
    sms_json = [{**x, "sms_1m_cash_key": cal_boy_type(x["sms_org_ok"], sms_1m_cash_key)} for x in sms_json]

    sms_json = [{**x, "day": (apply_time_int - cal_strdate_to_int(x["send_time"])) / 3600 / 1000 / 24} for x in
                sms_json]

    body = [x for x in sms_json]
    body_1d = [x for x in sms_json if x["day"] <= 1]
    body_3d = [x for x in sms_json if x["day"] <= 3]
    body_7d = [x for x in sms_json if x["day"] <= 7]
    body_15d = [x for x in sms_json if x["day"] <= 15]
    body_30d = [x for x in sms_json if x["day"] <= 30]
    body_60d = [x for x in sms_json if x["day"] <= 60]
    body_90d = [x for x in sms_json if x["day"] <= 90]
    body_360d = [x for x in sms_json if x["day"] <= 360]

    body_cnt = len(body)
    body_1d_cnt = len(body_1d)
    body_1d_rate = tmp_d(body_1d_cnt, body_cnt)
    body_3d_cnt = len(body_3d)
    body_3d_rate = tmp_d(body_3d_cnt, body_cnt)
    body_7d_cnt = len(body_7d)
    body_7d_rate = tmp_d(body_7d_cnt, body_cnt)
    body_15d_cnt = len(body_15d)
    body_15d_rate = tmp_d(body_15d_cnt, body_cnt)
    body_30d_cnt = len(body_30d)
    body_30d_rate = tmp_d(body_30d_cnt, body_cnt)
    body_60d_cnt = len(body_60d)
    body_60d_rate = tmp_d(body_60d_cnt, body_cnt)
    body_90d_cnt = len(body_90d)
    body_90d_rate = tmp_d(body_90d_cnt, body_cnt)
    body_360d_cnt = len(body_360d)
    body_360d_rate = tmp_d(body_360d_cnt, body_cnt)

    body_overdue = [x['sms_content_ok'] for x in sms_json if x["sms_overdue_key"] == 1 and x['sms_loan_key'] == 0
                    and x['sms_repayment_key'] == 0 and x['sms_expire_key'] == 0]
    body_overdue_cnt = len(body_overdue)
    body_overdue_rate = tmp_d(body_overdue_cnt, body_cnt)
    body_7d_overdue = [x['sms_content_ok'] for x in sms_json if x["sms_overdue_key"] == 1 and x['sms_loan_key'] == 0
                       and x['sms_repayment_key'] == 0 and x['sms_expire_key'] == 0 and x["day"] <= 7]
    body_7d_overdue_cnt = len(body_7d_overdue)
    body_90d_overdue = [x['sms_content_ok'] for x in sms_json if x["sms_overdue_key"] == 1 and x['sms_loan_key'] == 0
                        and x['sms_repayment_key'] == 0 and x['sms_expire_key'] == 0 and x["day"] <= 90]
    body_90d_overdue_cnt = len(body_90d_overdue)
    body_overdue_7d_div_90d = tmp_d(body_7d_overdue_cnt, body_90d_overdue_cnt)
    body_15d_overdue_serious_1m_cash = [x['sms_content_ok'] for x in sms_json if
                                        x["sms_overdue_serious_key"] == 1 and x["sms_1m_cash_key"] == 1 and x[
                                            "day"] <= 15]
    body_15d_overdue_serious_1m_cash_cnt = len(body_15d_overdue_serious_1m_cash)
    body_overdue_serious_1m_cash = [x['sms_content_ok'] for x in sms_json if
                                    x["sms_overdue_serious_key"] == 1 and x["sms_1m_cash_key"] == 1]
    body_overdue_serious_1m_cash_cnt = len(body_overdue_serious_1m_cash)
    body_15d_overdue_serious_1m_cash_div_overdue_serious_1m_cash = tmp_d(body_15d_overdue_serious_1m_cash_cnt,
                                                                         body_overdue_serious_1m_cash_cnt)
    body_7d_overdue = [x['sms_content_ok'] for x in sms_json if x["sms_overdue_key"] == 1 and x['sms_loan_key'] == 0
                       and x['sms_repayment_key'] == 0 and x['sms_expire_key'] == 0 and x["day"] <= 7]
    body_7d_overdue_cnt = len(body_7d_overdue)
    body_7d_overdue_rate = tmp_d(body_7d_overdue_cnt, body_7d_cnt)
    body_30d_overdue_serious = [x['sms_content_ok'] for x in sms_json if
                                x["sms_overdue_serious_key"] == 1 and x["day"] <= 30]
    body_30d_overdue_serious_cnt = len(body_30d_overdue_serious)
    body_30d_overdue_serious_rate = tmp_d(body_30d_overdue_serious_cnt, body_30d_cnt)
    body_360d_verif = [x['sms_content_ok'] for x in sms_json if x["sms_verif_key"] == 1 and x["day"] <= 360]
    body_360d_verif_cnt = len(body_360d_verif)
    body_30d_verif = [x['sms_content_ok'] for x in sms_json if x["sms_verif_key"] == 1 and x["day"] <= 30]
    body_30d_verif_cnt = len(body_30d_verif)
    body_verif_30d_div_360d = tmp_d(body_30d_verif_cnt, body_360d_verif_cnt)
    body_60d_1m_cash_verif = [x['sms_content_ok'] for x in sms_json if
                              x["sms_verif_key"] == 1 and x["sms_1m_cash_key"] == 1 and x["day"] <= 60]
    body_60d_1m_cash_verif_cnt = len(body_60d_1m_cash_verif)
    body_60d_1m_cash_verif_rate = tmp_d(body_60d_1m_cash_verif_cnt, body_60d_cnt)
    body_1m_cash_verif = [x['sms_content_ok'] for x in sms_json if
                          x["sms_verif_key"] == 1 and x["sms_1m_cash_key"] == 1]
    body_1m_cash_verif_cnt = len(body_1m_cash_verif)
    body_60d_1m_cash_verif_div_1m_cash_verif = tmp_d(body_60d_1m_cash_verif_cnt, body_1m_cash_verif_cnt)
    body_60d_overdue_serious_1m_cash = [x['sms_content_ok'] for x in sms_json if
                                        x["sms_overdue_serious_key"] == 1 and x["sms_1m_cash_key"] == 1 and x[
                                            "day"] <= 60]
    body_60d_overdue_serious_1m_cash_cnt = len(body_60d_overdue_serious_1m_cash)
    body_overdue_serious_1m_cash_15d_div_60d = tmp_d(body_15d_overdue_serious_1m_cash_cnt,
                                                     body_60d_overdue_serious_1m_cash_cnt)
    body_30d_bill = [x['sms_content_ok'] for x in sms_json if x["sms_bill_key"] == 1 and x["day"] <= 30]
    body_30d_bill_cnt = len(body_30d_bill)
    body_60d_bill = [x['sms_content_ok'] for x in sms_json if x["sms_bill_key"] == 1 and x["day"] <= 60]
    body_60d_bill_cnt = len(body_60d_bill)
    body_bill_30d_div_60d = tmp_d(body_30d_bill_cnt, body_60d_bill_cnt)
    body_3d_verif = [x['sms_content_ok'] for x in sms_json if x["sms_verif_key"] == 1 and x["day"] <= 3]
    body_3d_verif_cnt = len(body_3d_verif)
    body_15d_verif = [x['sms_content_ok'] for x in sms_json if x["sms_verif_key"] == 1 and x["day"] <= 15]
    body_15d_verif_cnt = len(body_15d_verif)
    body_verif_3d_div_15d = tmp_d(body_3d_verif_cnt, body_15d_verif_cnt)


    """4 借还款的数据集做特征工程"""
    """ beh """

    apply_time_tmp = str(params['apply_time'])
    # apply_time = datetime.datetime.strptime(ApplyDate, '%Y-%m-%d %H:%M:%S').timestamp()  # 时间戳

    # loan_time = str(params['loan_time'])
    # id_number = params['id_number']


    def subtract_days_from_date_str(date_str, days):
        date_format = "%Y-%m-%d %H:%M:%S"
        date_obj = datetime.datetime.strptime(date_str, date_format)
        new_date_obj = date_obj - datetime.timedelta(days=days)
        new_date_str = new_date_obj.strftime(date_format)
        return new_date_str

    day_1 = subtract_days_from_date_str(ApplyDate, 1)
    day_3 = subtract_days_from_date_str(ApplyDate, 3)
    day_5 = subtract_days_from_date_str(ApplyDate, 5)
    day_7 = subtract_days_from_date_str(ApplyDate, 7)
    day_15 = subtract_days_from_date_str(ApplyDate, 15)
    day_30 = subtract_days_from_date_str(ApplyDate, 30)
    day_60 = subtract_days_from_date_str(ApplyDate, 60)
    day_90 = subtract_days_from_date_str(ApplyDate, 90)
    day_360 = subtract_days_from_date_str(ApplyDate, 360)


    # df_phone = df_apply_all[(df_apply_all['id_number'] == id_number)
    #                         & (df_apply_all['created_at'] <= created_at)
    #                         & (df_apply_all['order_id'] != int(order_id))
    #                         & (df_apply_all['loan_time'] >= '2000')
    #                         ]

    #20240827 df_phone 逻辑修改
    df_phone = df_apply_all[(df_apply_all['id_number'] == id_number)
                            & (df_apply_all['created_at'] <= created_at)
                            & (df_apply_all['order_id'] != int(order_id))
                            ]

    df_apply_all[(df_apply_all['id_number'] == id_number)][['created_at', 'loan_time', 'fact_time']].sort_values(
        by=['created_at'])

    df_phone = df_phone.sort_values(by=['created_at'])
    df_phone[['created_at', "ReDate", 'loan_time', 'fact_time']]
    tmp = df_phone[(df_phone['fact_time'] >= apply_time_tmp) | (df_phone['fact_time'] == "NaT") | (
                df_phone['fact_time'] == "None")]

    loan_package_cnt = len(tmp['ProjectId'].unique())

    tmp['plan_repayment_amount'] = tmp['plan_repayment_amount'].astype('int64')
    if tmp.shape[0]:
        loan_last_amount = list(tmp['plan_repayment_amount'])[-1]
        loan_last_amount = int(loan_last_amount)
        loan_product_amount_group = tmp.groupby(['ProjectId'])['plan_repayment_amount'].sum()
        loan_product_amount_max = loan_product_amount_group.max()
        loan_product_amount_max = int(loan_product_amount_max)
        loan_amount_sum = tmp['plan_repayment_amount'].sum()
        loan_amount_sum = int(loan_amount_sum)
    else:
        loan_last_amount = 0
        loan_amount_sum= 0
        loan_product_amount_max = None


    """"""
    # df_phone = df_apply_all[(df_apply_all['id_number'] == id_number)
    #                         & (df_apply_all['created_at'] <= created_at)
    #                         & (df_apply_all['order_id'] != int(order_id))
    #                         & (df_apply_all['loan_time'] >= "2000")
    #                         ]

    # 20240827df_phone的逻辑修改
    df_phone = df_apply_all[(df_apply_all['id_number'] == id_number)
                            & (df_apply_all['created_at'] <= created_at)
                            & (df_apply_all['order_id'] != int(order_id))
                            ]
    
    df_phone = df_phone.sort_values(by=['created_at'])
    df_phone[['created_at', "ReDate", 'loan_time', 'fact_time']]
    tmp = df_phone[(df_phone['fact_time'] > '2000') & (df_phone['fact_time'] <= apply_time_tmp)]
    tmp['plan_repayment_amount'] = tmp['plan_repayment_amount'].astype('int64')
    settle_amount_sum = tmp['plan_repayment_amount'].sum()
    settle_amount_sum = int(settle_amount_sum)

    if tmp.shape[0]:
        settle_last_product_amount = list(tmp['plan_repayment_amount'])[-1]
        refund_day_max = tmp['fact_day'].max()
        refund_day_min = tmp['fact_day'].min()

        refund_day_max = datetime.datetime.strptime(refund_day_max, "%Y-%m-%d")
        refund_day_min = datetime.datetime.strptime(refund_day_min, "%Y-%m-%d")

        fact_day_diff = (refund_day_max - refund_day_min).days
    else:
        settle_last_product_amount = None
        fact_day_diff = None

    tmp_product = tmp[tmp['ProjectId'] == ProjectId]
    if tmp_product.shape[0]:
        last_self_product_overdue_days = list(tmp_product['overdue_days_xw'])[-1]
    else:
        last_self_product_overdue_days = None

    if tmp.shape[0]:
        last_overdue_days = list(tmp['overdue_days_xw'])[-1]

    else:
        print(order_id, "无结清天数")
        last_overdue_days = -999

    tmp['plan_repayment_amount'] = tmp['plan_repayment_amount'].astype('int64')
    settle_amount_mean = tmp['plan_repayment_amount'].mean()
    settle_product_amount_group = tmp.groupby(['ProjectId'])['plan_repayment_amount'].sum()
    settle_product_amount_mean = settle_product_amount_group.mean()
    settle_product_amount_mean = int(settle_product_amount_mean)
    settle_product_amount_min = settle_product_amount_group.min()
    settle_product_amount_min = int(settle_product_amount_min)

    overdue_days_sum = tmp['overdue_days_xw'].sum()

    tmp_15day = tmp[tmp['created_at'] >= day_15]
    if tmp_15day.shape[0]:
        overdue_15day_max = max(tmp_15day['overdue_days_xw'])
        overdue_15day_min = min(tmp_15day['overdue_days_xw'])
        overdue_15day_mean = sum(tmp_15day['overdue_days_xw']) / tmp_15day.shape[0]

    else:
        overdue_15day_max = None
        overdue_15day_min = None
        overdue_15day_mean = None

    tmp_30day = tmp[tmp['created_at'] >= day_30]
    if tmp_30day.shape[0]:
        overdue_30day_max = max(tmp_30day['overdue_days_xw'])
        overdue_30day_min = min(tmp_30day['overdue_days_xw'])
        overdue_30day_mean = sum(tmp_30day['overdue_days_xw']) / tmp_30day.shape[0]

    else:
        overdue_30day_max = None
        overdue_30day_min = None
        overdue_30day_mean = None

    tmp_60day = tmp[tmp['created_at'] >= day_60]
    if tmp_60day.shape[0]:
            overdue_60day_max = max(tmp_60day['overdue_days_xw'])
            overdue_60day_min = min(tmp_60day['overdue_days_xw'])
            overdue_60day_mean = sum(tmp_60day['overdue_days_xw']) / tmp_60day.shape[0]
    else:
            overdue_60day_max = None
            overdue_60day_min = None
            overdue_60day_mean = None
    tmp_90day = tmp[tmp['created_at'] >= day_90]
    if tmp_90day.shape[0]:
            overdue_90day_max = max(tmp_90day['overdue_days_xw'])
            overdue_90day_min = min(tmp_90day['overdue_days_xw'])
            overdue_90day_mean = sum(tmp_90day['overdue_days_xw']) / tmp_90day.shape[0]
    else:
            overdue_90day_max = None
            overdue_90day_min = None
            overdue_90day_mean = None

    overdue_days_amount = tmp.groupby(['overdue_days_xw'])['plan_repayment_amount'].sum()
    overdue_days_amount_min = overdue_days_amount.min()
    overdue_days_amount_min = int(overdue_days_amount_min)

    """# 还款时间和申请时间  要包含当前申请件   测试 ：      loan_record_id =   ********************************** **********************"""
    # df_phone = df_apply_all[(df_apply_all['id_number'] == id_number)
    #                         & (df_apply_all['created_at'] <= created_at)
    #                         & (df_apply_all['loan_time'] >= "2000")
    #                         ]  # 包含当前申请

    # 20240827逻辑修改
    df_phone = df_apply_all[(df_apply_all['id_number'] == id_number)
                            & (df_apply_all['created_at'] <= created_at)
                            ]  # 包含当前申请
    
    df_phone = df_phone.sort_values(by=['created_at'])

    tmp = df_phone[(df_phone['fact_time'] <= df_phone['loan_time']) | (df_phone['order_id'] == order_id)]

    # 多包分组 排序计算
    hours_list = []
    for app_id in set(list(tmp['ProjectId'])):
        df_app_id = tmp[tmp['ProjectId'] == app_id]

        apply_time_list = list(df_app_id['created_at'])
        apply_time_list = [str(x)[:19] for x in apply_time_list]
        apply_time_list = apply_time_list[1:]
        apply_time_stamp_list = [cal_timestamp(cal_strdate_to_int(x)  ) for x in apply_time_list]

        refund_time_list = list(df_app_id['fact_time'])
        refund_time_list = [str(x)[:19] for x in refund_time_list]
        refund_time_list = refund_time_list[:-1]
        refund_time_stamp_list = [cal_timestamp(cal_strdate_to_int(x)  ) for x in refund_time_list]

        hours = list(map(lambda x: cal_hours(x[0], x[1]), zip(apply_time_stamp_list, refund_time_stamp_list)))
        hours_list.extend(hours)

    if hours_list:
        max_apply_fact_hours = max(hours_list)
        min_apply_fact_hours = min(hours_list)
        mean_apply_fact_hours = sum(hours_list) / len(hours_list)
    else:
        max_apply_fact_hours = None
        min_apply_fact_hours = None
        mean_apply_fact_hours = None


    """# 申请时间和申请时间  要包含当前申请件   测试 ：      loan_record_id =   ********************************** **********************"""
    # df_phone = df_apply_all[(df_apply_all['id_number'] == id_number)
    #                         & (df_apply_all['created_at'] <= created_at)
    #                         & (df_apply_all['loan_time'] >= "2000")]  # 包含当前申请
    
    # 20240827代码逻辑修改
    df_phone = df_apply_all[(df_apply_all['id_number'] == id_number)
                            & (df_apply_all['created_at'] <= created_at)
                            ]  # 包含当前申请
    
    df_phone = df_phone.sort_values(by=['created_at'])

    tmp = df_phone

    apply_time_list_0 = list(tmp['created_at'])
    apply_time_list_0 = [str(x)[:19] for x in apply_time_list_0]
    apply_time_list_0 = apply_time_list_0[0:-1]
    apply_time_stamp_list_0 = [cal_timestamp(cal_strdate_to_int(x) ) for x in apply_time_list_0]

    apply_time_list_1 = list(tmp['created_at'])
    apply_time_list_1 = [str(x)[:19] for x in apply_time_list_1]
    apply_time_list_1 = apply_time_list_1[1:]
    apply_time_stamp_list_1 = [cal_timestamp(cal_strdate_to_int(x)  ) for x in apply_time_list_1]

    apply_apply_hours = list(
        map(lambda x: cal_hours(x[0], x[1]), zip(apply_time_stamp_list_1, apply_time_stamp_list_0)))
    if apply_apply_hours:
        max_apply_apply_hours = max(apply_apply_hours)
        min_apply_apply_hours = min(apply_apply_hours)
        mean_apply_apply_hours = sum(apply_apply_hours) / len(apply_apply_hours)
        last_apply_apply_hours = apply_apply_hours[-1]
    else:
        max_apply_apply_hours = None
        min_apply_apply_hours = None
        mean_apply_apply_hours = None
        last_apply_apply_hours = None

    loan_amount_div_repay_amount = tmp_d(loan_amount_sum, settle_amount_sum)
    # print(loan_amount_sum)
    # print(settle_amount_sum)

    model_variables = {

        "app_cash_1d_div_30d_50w_allday": app_cash_1d_div_30d_50w_allday,
        "app_cash_90d_div_180d_2m_allday": app_cash_90d_div_180d_2m_allday,
        "app_cash_1d_div_90d_50w_allday": app_cash_1d_div_90d_50w_allday,
        "age": age,
        "Maritalstatus": Maritalstatus,
        "body_overdue_rate": body_overdue_rate,
        "body_overdue_7d_div_90d": body_overdue_7d_div_90d,
        "body_15d_overdue_serious_1m_cash_div_overdue_serious_1m_cash": body_15d_overdue_serious_1m_cash_div_overdue_serious_1m_cash,
        "body_7d_overdue_rate": body_7d_overdue_rate,
        "body_30d_overdue_serious_rate": body_30d_overdue_serious_rate,
        "body_cnt": body_cnt,
        "body_360d_verif_cnt": body_360d_verif_cnt,
        "body_verif_30d_div_360d": body_verif_30d_div_360d,
        "body_60d_1m_cash_verif_div_1m_cash_verif": body_60d_1m_cash_verif_div_1m_cash_verif,
        "body_overdue_serious_1m_cash_15d_div_60d": body_overdue_serious_1m_cash_15d_div_60d,
        "body_360d_cnt": body_360d_cnt,
        "body_bill_30d_div_60d": body_bill_30d_div_60d,
        "body_verif_3d_div_15d": body_verif_3d_div_15d,
        "fact_day_diff": fact_day_diff,
        "last_apply_apply_hours": last_apply_apply_hours,
        "loan_amount_div_repay_amount": loan_amount_div_repay_amount,
        "loan_last_amount": loan_last_amount,
        "loan_product_amount_max": loan_product_amount_max,
        "max_apply_apply_hours": max_apply_apply_hours,
        "mean_apply_apply_hours": mean_apply_apply_hours,
        "min_apply_apply_hours": min_apply_apply_hours,
        "overdue_30day_min": overdue_30day_min,
        "overdue_60day_mean": overdue_60day_mean,
        "overdue_90day_mean": overdue_90day_mean,
        "overdue_days_sum": overdue_days_sum,
        "settle_amount_mean": settle_amount_mean,
        "settle_product_amount_mean": settle_product_amount_mean,
        "settle_product_amount_min": settle_product_amount_min,
        "app_7d_rate_inday": app_7d_rate_inday,
        "app_cash_30d_div_360d_46_inday": app_cash_30d_div_360d_46_inday,
        "app_cash_3d_div_30d_inday": app_cash_3d_div_30d_inday,
        "app_1d_div_30d_inday": app_1d_div_30d_inday,
        "app_cash_7d_div_30d_42_inday": app_cash_7d_div_30d_42_inday,
        "app_cash_3d_div_30d_2m_inday": app_cash_3d_div_30d_2m_inday,
        "app_cash_30d_div_90d_2m_inday": app_cash_30d_div_90d_2m_inday,
        "app_cash_7d_div_15d_50w_inday": app_cash_7d_div_15d_50w_inday,
        "body_7d_overdue_rate_org": body_7d_overdue_rate_org,
        "body_360d_cash_verif_rate_org": body_360d_cash_verif_rate_org,
        "body_15d_verif_rate_org": body_15d_verif_rate_org,
        "body_overdue_7d_div_30d_org": body_overdue_7d_div_30d_org,
        "body_60d_cash_verif_rate_org": body_60d_cash_verif_rate_org,
        "body_overdue_1m_cash_3d_div_7d_org": body_overdue_1m_cash_3d_div_7d_org,
        "body_overdue_serious_7d_div_360d_org": body_overdue_serious_7d_div_360d_org,
        "body_overdue_serious_3d_div_30d_org": body_overdue_serious_3d_div_30d_org,
        "body_expire_15d_div_360d_org": body_expire_15d_div_360d_org,
        "body_7d_loan_div_loan_org": body_7d_loan_div_loan_org,
        "body_overdue_topcash_1d_div_3d_org": body_overdue_topcash_1d_div_3d_org,
        "body_1d_upto_rate_tfidf": body_1d_upto_rate_tfidf,
        "body_7d_duedate_div_duedate_tfidf": body_7d_duedate_div_duedate_tfidf,
        "body_congrat_15d_div_60d_tfidf": body_congrat_15d_div_60d_tfidf,
        "body_advance_30d_div_360d_tfidf": body_advance_30d_div_360d_tfidf,
        "body_upto_15d_div_360d_tfidf": body_upto_15d_div_360d_tfidf,
        "body_discount_3d_div_30d_tfidf": body_discount_3d_div_30d_tfidf,
        "body_close_15d_div_60d_tfidf": body_close_15d_div_60d_tfidf,
        "body_90d_legal_cnt_tfidf": body_90d_legal_cnt_tfidf,
        "body_90d_discount_div_discount_tfidf": body_90d_discount_div_discount_tfidf,
        "body_3d_pastdue_div_pastdue_tfidf": body_3d_pastdue_div_pastdue_tfidf,
        "body_60d_collection_div_collection_tfidf": body_60d_collection_div_collection_tfidf,
        "body_360d_paid_cnt_tfidf": body_360d_paid_cnt_tfidf,
        "body_now_1d_div_7d_tfidf": body_now_1d_div_7d_tfidf,
        "body_pastdue_15d_div_90d_tfidf": body_pastdue_15d_div_90d_tfidf,
        "body_3d_https_div_https_tfidf": body_3d_https_div_https_tfidf,
        "body_paid_1d_div_3d_tfidf": body_paid_1d_div_3d_tfidf,
        "body_https_60d_div_90d_tfidf": body_https_60d_div_90d_tfidf,
        "body_https_3d_div_30d_tfidf": body_https_3d_div_30d_tfidf,
        "body_congrat_3d_div_15d_tfidf": body_congrat_3d_div_15d_tfidf,
        "body_now_3d_div_15d_tfidf": body_now_3d_div_15d_tfidf,
        "body_congrat_3d_div_7d_tfidf": body_congrat_3d_div_7d_tfidf,
        "body_duedate_cnt_tfidf": body_duedate_cnt_tfidf,
        "body_15d_close_div_close_tfidf": body_15d_close_div_close_tfidf,
        "body_360d_paid_div_paid_tfidf": body_360d_paid_div_paid_tfidf,
        "body_pastdue_3d_div_30d_tfidf": body_pastdue_3d_div_30d_tfidf,
        "body_reloan_3d_div_7d_tfidf": body_reloan_3d_div_7d_tfidf,
        "body_apply_15d_div_30d_tfidf": body_apply_15d_div_30d_tfidf,
        "app_cash_30d_div_180d_upday": app_cash_30d_div_180d_upday,
        "app_cash_1m_180d_rate_upday": app_cash_1m_180d_rate_upday,
        "app_cash_3d_rate_upday": app_cash_3d_rate_upday,
        "app_cash_7d_rate_upday": app_cash_7d_rate_upday,
        "app_inday_majority_upday": app_inday_majority_upday,
        "app_cash_3d_div_7d_48_upday": app_cash_3d_div_7d_48_upday,

    }

    df_tmp = pd.DataFrame(model_variables, index=[1])

    df_tmp.fillna(-999, inplace=True)

    var = [

        'loan_amount_div_repay_amount',
        'loan_product_amount_max',
        'loan_last_amount',
        'settle_amount_mean',
        'overdue_30day_min',
        'body_7d_overdue_rate_org',
        'app_cash_30d_div_180d_upday',
        'body_overdue_rate',
        'app_cash_1m_180d_rate_upday',
        'body_360d_cash_verif_rate_org',
        'mean_apply_apply_hours',
        'body_overdue_7d_div_90d',
        'app_7d_rate_inday',
        'app_cash_30d_div_360d_46_inday',
        'body_1d_upto_rate_tfidf',
        'body_15d_verif_rate_org',
        'age',
        'body_15d_overdue_serious_1m_cash_div_overdue_serious_1m_cash',
        'body_congrat_15d_div_60d_tfidf',
        'body_7d_overdue_rate',
        'body_7d_duedate_div_duedate_tfidf',
        'overdue_90day_mean',
        'min_apply_apply_hours',
        'app_cash_3d_rate_upday',
        'body_upto_15d_div_360d_tfidf',
        'app_cash_3d_div_30d_inday',
        'body_advance_30d_div_360d_tfidf',
        'overdue_days_sum',
        'body_30d_overdue_serious_rate',
        'body_cnt',
        'body_discount_3d_div_30d_tfidf',
        'body_verif_30d_div_360d',
        'body_close_15d_div_60d_tfidf',
        'last_apply_apply_hours',
        'body_overdue_7d_div_30d_org',
        'body_90d_discount_div_discount_tfidf',
        'body_360d_verif_cnt',
        'body_90d_legal_cnt_tfidf',
        'body_3d_pastdue_div_pastdue_tfidf',
        'fact_day_diff',
        'body_60d_1m_cash_verif_div_1m_cash_verif',
        'body_360d_paid_cnt_tfidf',
        'body_pastdue_15d_div_90d_tfidf',
        'overdue_60day_mean',
        'body_60d_collection_div_collection_tfidf',
        'app_1d_div_30d_inday',
        'Maritalstatus',
        'app_cash_7d_rate_upday',
        'body_now_1d_div_7d_tfidf',
        'body_bill_30d_div_60d',
        'body_paid_1d_div_3d_tfidf',
        'app_cash_7d_div_30d_42_inday',
        'body_3d_https_div_https_tfidf',
        'body_360d_cnt',
        'body_https_60d_div_90d_tfidf',
        'body_overdue_serious_7d_div_360d_org',
        'body_60d_cash_verif_rate_org',
        'body_now_3d_div_15d_tfidf',
        'body_https_3d_div_30d_tfidf',
        'app_cash_3d_div_30d_2m_inday',
        'body_overdue_1m_cash_3d_div_7d_org',
        'body_congrat_3d_div_7d_tfidf',
        'body_15d_close_div_close_tfidf',
        'body_reloan_3d_div_7d_tfidf',
        'body_overdue_serious_1m_cash_15d_div_60d',
        'app_inday_majority_upday',
        'body_duedate_cnt_tfidf',
        'body_congrat_3d_div_15d_tfidf',
        'body_360d_paid_div_paid_tfidf',
        'max_apply_apply_hours',
        'app_cash_1d_div_30d_50w_allday',
        'app_cash_3d_div_7d_48_upday',
        'body_pastdue_3d_div_30d_tfidf',
        'body_verif_3d_div_15d',
        'settle_product_amount_min',
        'settle_product_amount_mean',
        'body_apply_15d_div_30d_tfidf',
        'body_overdue_serious_3d_div_30d_org',
        'body_expire_15d_div_360d_org',
        'app_cash_30d_div_90d_2m_inday',
        'app_cash_1d_div_90d_50w_allday',
        'app_cash_90d_div_180d_2m_allday',
        'body_7d_loan_div_loan_org',
        'app_cash_7d_div_15d_50w_inday',
        'body_overdue_topcash_1d_div_3d_org'
    ]
    # print(df_tmp)

    file_path_name = 'genfang_new_api_v1.pkl'
    v1_old_model = get_pickle(file_path_name, request=None)

    # import pickle
    # with open('./new_api_v1.pkl', 'rb') as f:
    #     v1_old_model = pickle.load(f)

    preds  =v1_old_model.predict(df_tmp[var])
    gfNewScoreV1 = p2score(preds[0])
    # print(pd.to_dict(df_tmp))
    end_time = time.time()
    print('耗费时间：', end_time - start_time)
    
    result = {
        'gfNewScoreV1Model':model_variables,
        'gfNewScoreV1':gfNewScoreV1,
    }

    return result

def send_post_request(url, params):
    # 发送POST请求
    response = requests.post(url, data=params)
    
    # 检查请求是否成功
    if response.status_code == 200:
        # 解析JSON响应
        result = response.json()
        ProjectId = result.get("ProjectId")
        order_id = result.get("ApplyNO")
        apply_time = result.get("ApplyDate")
        app_data = result.get("AppList")
        sms_json = result.get("SmsList")
        birthday = result.get("Birthday")
        degree = result.get("Education")
        id_number = result.get("Phone")

        # 增加婚姻信息字段
        Maritalstatus = result.get("Maritalstatus")

        # 还款表
        id_number_json = result.get("id_number_json")
        
        params = {
            'ProjectId': ProjectId,
            'order_id': order_id,
            'apply_time': apply_time,
            'birthday': birthday,
            'degree': degree,
            'Maritalstatus': Maritalstatus,
            'app_data': app_data,
            'sms_json': sms_json,
            'id_number': id_number,
            'id_number_json': id_number_json,
        }
        return params
    else:
        print(f"Request failed with status code {response.status_code}")
        return None

# 测试样例
if __name__ == '__main__':
    url = "http://polo.philippines1.top/test/getRiskApiTestData"  # 替换为实际API的URL
    
    # val_df = pd.read_excel('./old_v1_oot_模型分_20240817.xlsx')
    # val_df['order_ids_1'] = val_df['order_ids'].str.replace('s','')

    # for ApplyNO in val_df.head(10)['order_ids_1'].tolist():
    for ApplyNO in ['172482962364492023']:
        
            params = {"ApplyNO": ApplyNO, "ApiType": "XWOldScoreV1"} 
            # 获取技术侧准备的数据集 
            test_params = send_post_request(url, params)
            print(test_params)
            # 获取线上计算的新客模型评分
            result = cal_genfang_new_v1(test_params)
            print(result)
            # gfNewScoreV1, df_tmp, order_id = cal_api_new_v1(test_params)
            # val_df_1 = val_df[val_df['order_ids'].str.contains(order_id)]
            # score_val = val_df_1['score'].tolist()[0]
            # if np.abs(gfNewScoreV1 - score_val) <= 0.1:
            #     print('订单ID', order_id, '无误', '线上评分：', gfNewScoreV1, '线下评分: ', score_val)
            # else:
            #     print('订单ID', order_id, '有偏差: ', np.abs(gfNewScoreV1 - score_val), '线上评分：', gfNewScoreV1, '线下评分: ', score_val)
            #     for var in df_tmp.columns.tolist():
            #         online_var_value = df_tmp[var].tolist()[0]
            #         offline_var_value = val_df_1[var].tolist()[0]
            #         if np.abs(online_var_value - offline_var_value) > 0.1:
            #             print('入模变量：', var, '线上计算值：', online_var_value, '线下计算值：', offline_var_value)

            # print("*"*100)
        # except Exception as e:
        #     print(f"订单ID {ApplyNO} 发生异常错误发生: {e}")
        #     print("*"*100)

# def send_post_request(url, params):
#     # 发送POST请求
#     response = requests.post(url, data=params)
    
#     # 检查请求是否成功
#     if response.status_code == 200:
#         # 解析JSON响应
#         result = response.json()
#         return result
#     else:
#         print(f"Request failed with status code {response.status_code}")
#         return None

# # 测试样例
# if __name__ == '__main__':
#     url = "http://polo.philippines1.top/test/getRiskApiTestData"  # 替换为实际API的URL
    
#     # val_df = pd.read_excel('./old_v1_oot_模型分_20240817.xlsx')
#     # val_df['order_ids_1'] = val_df['order_ids'].str.replace('s','')

#     # for ApplyNO in val_df.head(10)['order_ids_1'].tolist():
#     for ApplyNO in ['172111630565546915']:
#         try:
#             params = {"ApplyNO": ApplyNO, "ApiType": "XWOldScoreV1"} 
#             # 获取技术侧准备的数据集 
#             test_params = send_post_request(url, params)
#             # 获取线上计算的新客模型评分
#             result = cal_genfang_new_v1(test_params)
#             print(result)
#             # gfNewScoreV1, df_tmp, order_id = cal_api_new_v1(test_params)
#             # val_df_1 = val_df[val_df['order_ids'].str.contains(order_id)]
#             # score_val = val_df_1['score'].tolist()[0]
#             # if np.abs(gfNewScoreV1 - score_val) <= 0.1:
#             #     print('订单ID', order_id, '无误', '线上评分：', gfNewScoreV1, '线下评分: ', score_val)
#             # else:
#             #     print('订单ID', order_id, '有偏差: ', np.abs(gfNewScoreV1 - score_val), '线上评分：', gfNewScoreV1, '线下评分: ', score_val)
#             #     for var in df_tmp.columns.tolist():
#             #         online_var_value = df_tmp[var].tolist()[0]
#             #         offline_var_value = val_df_1[var].tolist()[0]
#             #         if np.abs(online_var_value - offline_var_value) > 0.1:
#             #             print('入模变量：', var, '线上计算值：', online_var_value, '线下计算值：', offline_var_value)

#             # print("*"*100)
#         except Exception as e:
#             print(f"订单ID {ApplyNO} 发生异常错误发生: {e}")
#             print("*"*100)
    



    # 2) 测试用例2-172101024075578163
    # test_params = send_post_request(url, params)
    # gfNewScoreV1, df_tmp, order_id = cal_api_new_v1(test_params)
    # print(gfNewScoreV1)
    
